From 54f8b3574dba72ac203de37db68887dec28160a5 Mon Sep 17 00:00:00 2001 From: Ly-sec Date: Mon, 10 Nov 2025 18:21:47 +0100 Subject: [PATCH] KeyboardLayout: add toast to inform the user about changed layout --- Assets/Translations/de.json | 3 ++ Assets/Translations/en.json | 3 ++ Assets/Translations/es.json | 3 ++ Assets/Translations/fr.json | 3 ++ Assets/Translations/pt.json | 3 ++ Assets/Translations/tr.json | 3 ++ Assets/Translations/uk-UA.json | 3 ++ Assets/Translations/zh-CN.json | 3 ++ Services/Keyboard/KeyboardLayoutService.qml | 54 +++++++++++++++++---- 9 files changed, 68 insertions(+), 10 deletions(-) diff --git a/Assets/Translations/de.json b/Assets/Translations/de.json index d116b8ce..e5d265ad 100644 --- a/Assets/Translations/de.json +++ b/Assets/Translations/de.json @@ -1736,6 +1736,9 @@ "disabled": "Deaktiviert", "enabled": "Aktiviert" }, + "keyboard-layout": { + "changed": "Tastaturlayout geändert zu {layout}" + }, "kofi": { "opened": "Ko-fi-Seite in Ihrem Browser geöffnet" }, diff --git a/Assets/Translations/en.json b/Assets/Translations/en.json index 3197e2bd..be165d3d 100644 --- a/Assets/Translations/en.json +++ b/Assets/Translations/en.json @@ -1736,6 +1736,9 @@ "disabled": "Disabled", "enabled": "Enabled" }, + "keyboard-layout": { + "changed": "Keyboard layout changed to {layout}" + }, "kofi": { "opened": "Ko-fi page opened in your browser" }, diff --git a/Assets/Translations/es.json b/Assets/Translations/es.json index 16677a9d..aa144830 100644 --- a/Assets/Translations/es.json +++ b/Assets/Translations/es.json @@ -1736,6 +1736,9 @@ "disabled": "Desactivado", "enabled": "Activado" }, + "keyboard-layout": { + "changed": "Distribución de teclado cambiada a {layout}" + }, "kofi": { "opened": "Página de Ko-fi abierta en tu navegador" }, diff --git a/Assets/Translations/fr.json b/Assets/Translations/fr.json index 17facb56..e764f9f6 100644 --- a/Assets/Translations/fr.json +++ b/Assets/Translations/fr.json @@ -1736,6 +1736,9 @@ "disabled": "Désactivé", "enabled": "Activé" }, + "keyboard-layout": { + "changed": "Disposition du clavier changée vers {layout}" + }, "kofi": { "opened": "Page Ko-fi ouverte dans votre navigateur" }, diff --git a/Assets/Translations/pt.json b/Assets/Translations/pt.json index 46e3666f..5eb685eb 100644 --- a/Assets/Translations/pt.json +++ b/Assets/Translations/pt.json @@ -1736,6 +1736,9 @@ "disabled": "Desativado", "enabled": "Ativado" }, + "keyboard-layout": { + "changed": "Layout de teclado alterado para {layout}" + }, "kofi": { "opened": "Página do Ko-fi aberta no seu navegador" }, diff --git a/Assets/Translations/tr.json b/Assets/Translations/tr.json index 01e237db..667db095 100644 --- a/Assets/Translations/tr.json +++ b/Assets/Translations/tr.json @@ -1736,6 +1736,9 @@ "disabled": "Devre dışı", "enabled": "Etkin" }, + "keyboard-layout": { + "changed": "Klavye düzeni {layout} olarak değiştirildi" + }, "kofi": { "opened": "Ko-fi sayfası tarayıcınızda açıldı" }, diff --git a/Assets/Translations/uk-UA.json b/Assets/Translations/uk-UA.json index 3a16c814..65b3f067 100644 --- a/Assets/Translations/uk-UA.json +++ b/Assets/Translations/uk-UA.json @@ -1736,6 +1736,9 @@ "disabled": "Вимкнено", "enabled": "Увімкнено" }, + "keyboard-layout": { + "changed": "Розкладка клавіатури змінена на {layout}" + }, "kofi": { "opened": "Сторінка Ko-fi відкрита у вашому браузері" }, diff --git a/Assets/Translations/zh-CN.json b/Assets/Translations/zh-CN.json index ab79009d..ecc12cb8 100644 --- a/Assets/Translations/zh-CN.json +++ b/Assets/Translations/zh-CN.json @@ -1736,6 +1736,9 @@ "disabled": "已禁用", "enabled": "已启用" }, + "keyboard-layout": { + "changed": "键盘布局已更改为 {layout}" + }, "kofi": { "opened": "Ko-fi 页面已在您的浏览器中打开" }, diff --git a/Services/Keyboard/KeyboardLayoutService.qml b/Services/Keyboard/KeyboardLayoutService.qml index a28ff993..5db74868 100644 --- a/Services/Keyboard/KeyboardLayoutService.qml +++ b/Services/Keyboard/KeyboardLayoutService.qml @@ -5,10 +5,13 @@ import Quickshell import Quickshell.Io import qs.Commons import qs.Services.Compositor +import qs.Services.UI Singleton { id: root property string currentLayout: I18n.tr("system.unknown-layout") + property string previousLayout: "" + property bool isInitialized: false property int updateInterval: 1000 // Update every second // Timer to periodically update the layout @@ -34,7 +37,7 @@ Singleton { const layoutName = data.names[data.current_idx] root.currentLayout = extractLayoutCode(layoutName) } catch (e) { - root.currentLayout = "Unknown" + root.currentLayout = I18n.tr("system.unknown-layout") } } } @@ -54,10 +57,10 @@ Singleton { if (mainKeyboard && mainKeyboard.active_keymap) { root.currentLayout = extractLayoutCode(mainKeyboard.active_keymap) } else { - root.currentLayout = "Unknown" + root.currentLayout = I18n.tr("system.unknown-layout") } } catch (e) { - root.currentLayout = "Unknown" + root.currentLayout = I18n.tr("system.unknown-layout") } } } @@ -79,9 +82,9 @@ Singleton { return } } - root.currentLayout = "Unknown" + root.currentLayout = I18n.tr("system.unknown-layout") } catch (e) { - root.currentLayout = "Unknown" + root.currentLayout = I18n.tr("system.unknown-layout") } } } @@ -112,9 +115,9 @@ Singleton { } } } - root.currentLayout = "Unknown" + root.currentLayout = I18n.tr("system.unknown-layout") } catch (e) { - root.currentLayout = "Unknown" + root.currentLayout = I18n.tr("system.unknown-layout") } } } @@ -170,7 +173,7 @@ Singleton { // Extract layout code from various format strings using Commons data function extractLayoutCode(layoutString) { if (!layoutString) - return "Unknown" + return I18n.tr("system.unknown-layout") const str = layoutString.toLowerCase() @@ -197,12 +200,43 @@ Singleton { // If nothing matches, try first 2-3 characters if they look like a code const codeMatch = str.match(/^([a-z]{2,3})/) - return codeMatch ? codeMatch[1] : "unknown" + return codeMatch ? codeMatch[1] : I18n.tr("system.unknown-layout") + } + + // Watch for layout changes and show toast + onCurrentLayoutChanged: { + // Update previousLayout after checking for changes + const layoutChanged = isInitialized && currentLayout !== previousLayout && currentLayout !== I18n.tr("system.unknown-layout") && previousLayout !== "" && previousLayout !== I18n.tr("system.unknown-layout") + + if (layoutChanged) { + const message = I18n.tr("toast.keyboard-layout.changed", { + "layout": currentLayout.toUpperCase() + }) + ToastService.showNotice(message, "", "", 2000) + Logger.d("KeyboardLayout", "Layout changed from", previousLayout, "to", currentLayout) + } + + // Update previousLayout for next comparison + previousLayout = currentLayout } Component.onCompleted: { Logger.i("KeyboardLayout", "Service started") updateLayout() + // Mark as initialized after a delay to allow first layout update to complete + // This prevents showing a toast on the initial load + initializationTimer.start() + } + + Timer { + id: initializationTimer + interval: 2000 // Wait 2 seconds for first layout update to complete + onTriggered: { + isInitialized = true + // Set previousLayout to current value after initialization + previousLayout = currentLayout + Logger.d("KeyboardLayout", "Service initialized, current layout:", currentLayout) + } } function updateLayout() { @@ -225,7 +259,7 @@ Singleton { gsettingsProcess.running = true } } else { - currentLayout = "Unknown" + currentLayout = I18n.tr("system.unknown-layout") } } }