|
| 1 | +import QtQuick |
| 2 | +import QtQuick.Layouts |
| 3 | +import Quickshell |
| 4 | +import qs.Commons |
| 5 | +import qs.Modules.Bar.Extras |
| 6 | +import qs.Modules.Panels.Settings |
| 7 | +import qs.Services.UI |
| 8 | +import qs.Widgets |
| 9 | +import "." as Local |
| 10 | + |
| 11 | +Item { |
| 12 | + id: root |
| 13 | + |
| 14 | + property var pluginApi: null |
| 15 | + property var ipMonitorService: pluginApi?.mainInstance?.ipMonitorService || null |
| 16 | + |
| 17 | + property ShellScreen screen |
| 18 | + property string widgetId: "" |
| 19 | + property string section: "" |
| 20 | + |
| 21 | + property var cfg: pluginApi?.pluginSettings || ({}) |
| 22 | + property var defaults: pluginApi?.manifest?.metadata?.defaultSettings || ({}) |
| 23 | + |
| 24 | + readonly property string iconColorKey: cfg.iconColor ?? defaults.iconColor |
| 25 | + readonly property string successIconKey: cfg.successIcon ?? defaults.successIcon ?? "network" |
| 26 | + readonly property string errorIconKey: cfg.errorIcon ?? defaults.errorIcon ?? "alert-circle" |
| 27 | + readonly property string loadingIconKey: cfg.loadingIcon ?? defaults.loadingIcon ?? "loader" |
| 28 | + // Read IP state from service (service is the source of truth) |
| 29 | + readonly property string currentIp: ipMonitorService?.currentIp ?? "n/a" |
| 30 | + readonly property var ipData: ipMonitorService?.ipData ?? null |
| 31 | + readonly property string fetchState: ipMonitorService?.fetchState ?? "idle" |
| 32 | + |
| 33 | + readonly property string displayIp: currentIp |
| 34 | + readonly property bool isHot: fetchState === "success" |
| 35 | + |
| 36 | + // Bar position handling |
| 37 | + readonly property string screenName: screen ? screen.name : "" |
| 38 | + readonly property string barPosition: Settings.getBarPositionForScreen(screenName) |
| 39 | + readonly property bool isVerticalBar: barPosition === "left" || barPosition === "right" |
| 40 | + |
| 41 | + readonly property string currentIcon: { |
| 42 | + if (fetchState === "loading") return loadingIconKey; |
| 43 | + if (fetchState === "error") return errorIconKey; |
| 44 | + return successIconKey; |
| 45 | + } |
| 46 | + |
| 47 | + readonly property color iconColor: isHot ? Color.resolveColorKey(iconColorKey) : Color.mOnSurfaceVariant |
| 48 | + readonly property color textColor: isHot ? Color.mOnSurface : Color.mOnSurfaceVariant |
| 49 | + |
| 50 | + implicitWidth: pill.width |
| 51 | + implicitHeight: pill.height |
| 52 | + |
| 53 | + onIpMonitorServiceChanged: { |
| 54 | + Logger.d("IpMonitor", "BarWidget ipMonitorService changed:", ipMonitorService !== null); |
| 55 | + } |
| 56 | + |
| 57 | + onCurrentIpChanged: { |
| 58 | + Logger.d("IpMonitor", "BarWidget currentIp changed to:", currentIp); |
| 59 | + } |
| 60 | + |
| 61 | + Component.onCompleted: { |
| 62 | + Logger.d("IpMonitor", "BarWidget completed refresh"); |
| 63 | + } |
| 64 | + |
| 65 | + BarPill { |
| 66 | + id: pill |
| 67 | + screen: root.screen |
| 68 | + oppositeDirection: BarService.getPillDirection(root) |
| 69 | + icon: root.currentIcon |
| 70 | + text: root.displayIp |
| 71 | + rotateText: isVerticalBar |
| 72 | + forceOpen: true |
| 73 | + autoHide: false |
| 74 | + customTextIconColor: root.iconColor |
| 75 | + |
| 76 | + tooltipText: { |
| 77 | + var lines = []; |
| 78 | + lines.push("Left click: Open panel"); |
| 79 | + lines.push("Right click: Menu"); |
| 80 | + if (root.fetchState === "success" && root.ipData) { |
| 81 | + var data = root.ipData; |
| 82 | + lines.push(""); |
| 83 | + if (data.city || data.country) { |
| 84 | + var parts = []; |
| 85 | + if (data.city) parts.push(data.city); |
| 86 | + if (data.country) parts.push(data.country); |
| 87 | + lines.push(parts.join(", ")); |
| 88 | + } |
| 89 | + } |
| 90 | + return lines.join("\n"); |
| 91 | + } |
| 92 | + |
| 93 | + onClicked: { |
| 94 | + // Open panel (displays cached IP data) |
| 95 | + if (pluginApi) { |
| 96 | + pluginApi.openPanel(root.screen, root); |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + onRightClicked: { |
| 101 | + PanelService.showContextMenu(contextMenu, root, screen); |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + NPopupContextMenu { |
| 106 | + id: contextMenu |
| 107 | + |
| 108 | + model: [ |
| 109 | + { |
| 110 | + "label": "Copy IP", |
| 111 | + "action": "copy", |
| 112 | + "icon": "copy" |
| 113 | + }, |
| 114 | + { |
| 115 | + "label": "Refresh IP", |
| 116 | + "action": "refresh", |
| 117 | + "icon": "refresh" |
| 118 | + }, |
| 119 | + { |
| 120 | + "label": pluginApi?.tr("menu.settings"), |
| 121 | + "action": "settings", |
| 122 | + "icon": "settings" |
| 123 | + }, |
| 124 | + ] |
| 125 | + |
| 126 | + onTriggered: function (action) { |
| 127 | + contextMenu.close(); |
| 128 | + PanelService.closeContextMenu(screen); |
| 129 | + if (action === "copy") { |
| 130 | + if (currentIp && currentIp !== "n/a") { |
| 131 | + Quickshell.execDetached(["sh", "-c", `printf '%s' '${currentIp}' | wl-copy`]); |
| 132 | + ToastService.showNotice("IP copied to clipboard: " + currentIp); |
| 133 | + Logger.d("IpMonitor", "Copied IP to clipboard:", currentIp); |
| 134 | + } else { |
| 135 | + ToastService.showNotice("No IP to copy"); |
| 136 | + } |
| 137 | + } else if (action === "refresh") { |
| 138 | + ipMonitorService.fetchIp(); |
| 139 | + } else if (action === "settings") { |
| 140 | + BarService.openPluginSettings(root.screen, pluginApi.manifest); |
| 141 | + } |
| 142 | + } |
| 143 | + } |
| 144 | +} |
| 145 | + |
0 commit comments