Commit 4475ab6a authored by Andrey Butirsky's avatar Andrey Butirsky

fix: keyboard layout applet: no flag if Display Name is set

Reworked for new DBus API and event-driven plugin interface to QML.

Essentially, we now get only one DBus signal with id when the layout
changes.
All the other info applet is capable to lookup locally via it's own data
- no need for additionally DBus calls any more.

This is possible because new API provides exhaustive info about all
layouts via updated getLayoutsList() call - now it returns a vector of
all layouts with all names for each. This info is stored on QML side in
Actions objects - which at the same time serve to fill context menu.
parent cc77e4ff
......@@ -11,32 +11,53 @@ import org.kde.plasma.core 2.0 as PlasmaCore
import org.kde.plasma.workspace.components 2.0
KeyboardLayoutButton {
text: layout.layoutDisplayName
Plasmoid.toolTipSubText: layout.layoutLongName
icon.name: StandardPaths.locate(StandardPaths.GenericDataLocation,
"kf5/locale/countries/" + layout.layoutDisplayName + "/flag.png")
id: root
display: plasmoid.configuration.showFlag && icon.name ? AbstractButton.IconOnly : AbstractButton.TextOnly
Plasmoid.status: hasMultipleKeyboardLayouts ? PlasmaCore.Types.ActiveStatus : PlasmaCore.Types.HiddenStatus
Plasmoid.preferredRepresentation: Plasmoid.fullRepresentation
function iconURL(name) {
return StandardPaths.locate(StandardPaths.GenericDataLocation,
"kf5/locale/countries/" + name + "/flag.png")
}
connections.target: null
Connections {
target: layout
function onLayoutsChanged() {
target: keyboardLayout
function onLayoutsChanged(layouts) {
plasmoid.clearActions()
layout.layouts.forEach(
function(layoutID) {
// TODO: add layoutDisplayName to layouts and lookup icon.name by index here
plasmoid.setAction(layoutID, layoutID /* ,icon.name */)
layouts.forEach(
function(layout) {
plasmoid.setAction(
layout.id,
layout.longName,
iconURL(layout.shortName).toString().substring(7) // remove file:// scheme
)
const action = plasmoid.action(layout.id)
action.toolTip = layout.displayName || layout.shortName
action.iconText = layout.shortName
}
)
}
function onLayoutChanged(idName) {
const action = plasmoid.action(idName)
text = action.toolTip
root.Plasmoid.toolTipSubText = action.text
icon.name = iconURL(action.iconText)
root.Plasmoid.activated()
}
}
function actionTriggered(selectedLayout) {
Plasmoid.activated()
layout.layout = selectedLayout
keyboardLayout.setLayout(selectedLayout)
}
// to fit at least 2 letters in systray
......
......@@ -31,6 +31,7 @@ set( kded_keyboard_SRCS
layouts_menu.cpp
flags.cpp
xkb_rules.cpp
layoutnames.cpp
)
ecm_qt_declare_logging_category(kded_keyboard_SRCS
......
......@@ -58,6 +58,8 @@ KeyboardDaemon::KeyboardDaemon(QObject *parent, const QList<QVariant>&)
dbus.registerObject(KEYBOARD_DBUS_OBJECT_PATH, this, QDBusConnection::ExportScriptableSlots | QDBusConnection::ExportScriptableSignals);
dbus.connect(QString(), KEYBOARD_DBUS_OBJECT_PATH, KEYBOARD_DBUS_SERVICE_NAME, KEYBOARD_DBUS_CONFIG_RELOAD_MESSAGE, this, SLOT(configureKeyboard()));
LayoutNames::registerMetaType();
configureKeyboard();
registerListeners();
......@@ -236,19 +238,20 @@ QString KeyboardDaemon::getLayout() const
return currentLayout.toString();
}
QString KeyboardDaemon::getLayoutDisplayName() const
{
return Flags::getShortText(currentLayout, keyboardConfig);
}
QString KeyboardDaemon::getLayoutLongName() const
{
return Flags::getLongText(currentLayout, rules);
}
QStringList KeyboardDaemon::getLayoutsList() const
QVector<LayoutNames> KeyboardDaemon::getLayoutsList() const
{
return X11Helper::getLayoutsListAsString( X11Helper::getLayoutsList() );
QVector<LayoutNames> ret;
const auto layoutsList = X11Helper::getLayoutsList();
for (auto &layoutUnit : layoutsList) {
ret.append( {layoutUnit.toString(), layoutUnit.layout(), Flags::getShortText(layoutUnit, keyboardConfig), Flags::getLongText(layoutUnit, rules)} );
}
return ret;
}
#include "keyboard_daemon.moc"
......@@ -26,6 +26,7 @@
#include "layout_memory.h"
#include "keyboard_dbus.h"
#include "bindings.h"
#include "layoutnames.h"
class XInputEventNotifier;
class LayoutTrayIcon;
......@@ -62,9 +63,8 @@ public Q_SLOTS:
Q_SCRIPTABLE void switchToNextLayout();
Q_SCRIPTABLE bool setLayout(const QString& layout);
Q_SCRIPTABLE QString getLayout() const;
Q_SCRIPTABLE QString getLayoutDisplayName() const;
Q_SCRIPTABLE QString getLayoutLongName() const;
Q_SCRIPTABLE QStringList getLayoutsList() const;
Q_SCRIPTABLE QVector<LayoutNames> getLayoutsList() const;
Q_SIGNALS:
Q_SCRIPTABLE void layoutChanged(QString layout);
......
#include "layoutnames.h"
#include <QDBusMetaType>
void LayoutNames::registerMetaType()
{
qDBusRegisterMetaType<LayoutNames>();
qDBusRegisterMetaType<QVector<LayoutNames>>();
}
QDBusArgument &operator<<(QDBusArgument &argument, const LayoutNames &layoutNames)
{
argument.beginStructure();
argument << layoutNames.id << layoutNames.shortName << layoutNames.displayName << layoutNames.longName;
argument.endStructure();
return argument;
}
const QDBusArgument &operator>>(const QDBusArgument &argument, LayoutNames &layoutNames)
{
argument.beginStructure();
argument >> layoutNames.id >> layoutNames.shortName >> layoutNames.displayName >> layoutNames.longName;
argument.endStructure();
return argument;
}
#ifndef LAYOUTNAMES_H
#define LAYOUTNAMES_H
#include <QMetaType>
class QDBusArgument;
struct LayoutNames
{
static void registerMetaType();
QString id;
QString shortName;
QString displayName;
QString longName;
};
Q_DECLARE_METATYPE(LayoutNames)
QDBusArgument &operator<<(QDBusArgument &argument, const LayoutNames &layoutNames);
const QDBusArgument &operator>>(const QDBusArgument &argument, LayoutNames &layoutNames);
#endif // LAYOUTNAMES_H
......@@ -19,7 +19,7 @@ endmacro(KEYBOARD_DAEMON_UNIT_TESTS)
keyboard_daemon_unit_tests(xkb_rules)
keyboard_daemon_unit_tests(iso_codes)
add_executable(flags_test flags_test.cpp ../flags.cpp ../x11_helper.cpp ../keyboard_config.cpp ../xkb_rules.cpp ../debug.cpp)
add_executable(flags_test flags_test.cpp ../flags.cpp ../x11_helper.cpp ../keyboard_config.cpp ../xkb_rules.cpp ../debug.cpp ../layoutnames.cpp)
ecm_mark_nongui_executable(flags_test)
ecm_mark_as_test(flags_test)
add_test(NAME kcm-keyboard-flags_test COMMAND flags_test)
......@@ -28,6 +28,7 @@ target_link_libraries(flags_test
Qt5::X11Extras
Qt5::Xml
Qt5::Test
Qt5::DBus
Qt5::Widgets
KF5::GuiAddons
KF5::I18n
......@@ -44,7 +45,7 @@ configure_file(${CMAKE_CURRENT_SOURCE_DIR}/config/base.bad.xml ${CMAKE_CURRENT_B
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/config/base.xml ${CMAKE_CURRENT_BINARY_DIR}/config/base.xml COPYONLY)
if( X11_XTest_FOUND )
add_executable(keyboard_daemon_test keyboard_daemon_test.cpp ../keyboard_daemon.cpp ../layout_memory.cpp ../layout_memory_persister.cpp ../flags.cpp ../udev_helper.cpp ../x11_helper.cpp ../xkb_helper.cpp ../xinput_helper.cpp ../layout_tray_icon.cpp ../keyboard_config.cpp ../xkb_rules.cpp ../keyboard_hardware.cpp ../bindings.cpp ../layouts_menu.cpp ../debug.cpp)
add_executable(keyboard_daemon_test keyboard_daemon_test.cpp ../keyboard_daemon.cpp ../layout_memory.cpp ../layout_memory_persister.cpp ../flags.cpp ../udev_helper.cpp ../x11_helper.cpp ../xkb_helper.cpp ../xinput_helper.cpp ../layout_tray_icon.cpp ../keyboard_config.cpp ../xkb_rules.cpp ../keyboard_hardware.cpp ../bindings.cpp ../layouts_menu.cpp ../debug.cpp ../layoutnames.cpp)
add_test(NAME kcm-keyboard-keyboard_daemon_test COMMAND keyboard_daemon_test)
add_dependencies(keyboard_daemon_test kded_keyboard)
ecm_mark_nongui_executable(keyboard_daemon_test)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment