summaryrefslogtreecommitdiff
path: root/accounts/gkleen@sif/shell/quickshell/KeyboardLayout.qml
blob: 4a6b8390bc08ea921d6fb0d3372a99c17cb15441 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import Quickshell
import QtQuick
import qs.Services
import Quickshell.Widgets

Rectangle {
  id: kbdWidget

  property var keyboardAbbrev: { "English (programmer Dvorak)": "dvp", "English (US)": "us" }

  width: kbdLabel.contentWidth + 8
  color: {
    if (kbdMouseArea.containsMouse) {
      return "#33808080";
    }
    return "transparent";
  }
  height: parent.height
  anchors.verticalCenter: parent.verticalCenter

  WrapperMouseArea {
    id: kbdMouseArea

    anchors.fill: parent
    hoverEnabled: true
    cursorShape: Qt.PointingHandCursor
    enabled: true
    onClicked: {
      NiriService.sendCommand({ "Action": { "SwitchLayout": { "layout": "Next" } } }, _ => {})
    }
    onWheel: event => {
      NiriService.sendCommand({ "Action": { "SwitchLayout": { "layout": event.angleDelta > 0 ? "Next" : "Prev" } } }, _ => {})
    }

    Text {
      id: kbdLabel

      font.pointSize: 10
      font.family: "Fira Sans"
      color: {
	if (NiriService.keyboardLayouts?.current_idx === 0)
	  return "#555";
	return "white";
      }
      anchors.centerIn: parent

      text: {
	const currentLayout = NiriService.keyboardLayouts?.names?.[NiriService.keyboardLayouts.current_idx];
	if (!currentLayout)
	  return "";
	return kbdWidget.keyboardAbbrev[currentLayout] ? kbdWidget.keyboardAbbrev[currentLayout] : currentLayout;
      }
    }
  }

  PopupWindow {
    id: tooltip

    property bool nextVisible: kbdMouseArea.containsMouse || tooltipMouseArea.containsMouse

    anchor {
      item: kbdMouseArea
      edges: Edges.Bottom | Edges.Left
    }
    visible: false

    onNextVisibleChanged: hangTimer.restart()

    Timer {
      id: hangTimer
      interval: 100
      onTriggered: tooltip.visible = tooltip.nextVisible
    }

    implicitWidth: kbdTooltipText.contentWidth + 16
    implicitHeight: kbdTooltipText.contentHeight + 16
    color: "black"

    WrapperMouseArea {
      id: tooltipMouseArea

      hoverEnabled: true
      enabled: true

      anchors.centerIn: parent

      Text {
	id: kbdTooltipText

	font.pointSize: 10
	font.family: "Fira Sans"
	color: "white"

	text: {
	  const currentLayout = NiriService.keyboardLayouts?.names?.[NiriService.keyboardLayouts.current_idx];
	  return currentLayout || "";
	}
      }
    }
  }
}