|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "time" |
| 6 | + |
| 7 | + "github.com/MiracleOS-Team/desktoplib/batteryHandler" |
| 8 | + "github.com/MiracleOS-Team/desktoplib/foreignToplevel" |
| 9 | + "github.com/MiracleOS-Team/desktoplib/networkManagerHandler" |
| 10 | + "github.com/MiracleOS-Team/desktoplib/volumeHandler" |
| 11 | + "github.com/dlasky/gotk3-layershell/layershell" |
| 12 | + "github.com/gotk3/gotk3/gdk" |
| 13 | + "github.com/gotk3/gotk3/glib" |
| 14 | + "github.com/gotk3/gotk3/gtk" |
| 15 | +) |
| 16 | + |
| 17 | +func getDateInfo() (string, string) { |
| 18 | + hours, minutes, _ := time.Now().Clock() |
| 19 | + curTimeInString := fmt.Sprintf("%d:%02d", hours, minutes) |
| 20 | + |
| 21 | + curDay := time.Now().Day() |
| 22 | + curDayName := firstN(time.Now().Weekday().String(), 3) |
| 23 | + var curDayCal string |
| 24 | + |
| 25 | + if curDay <= 5 { |
| 26 | + curMonth := time.Now().Month() |
| 27 | + curDayCal = fmt.Sprintf("%s. %02d %s", curDayName, curDay, curMonth) |
| 28 | + } else { |
| 29 | + curDayCal = fmt.Sprintf("%s. %02d", curDayName, curDay) |
| 30 | + } |
| 31 | + return curDayCal, curTimeInString |
| 32 | +} |
| 33 | + |
| 34 | +func createSidestuff() *gtk.Box { |
| 35 | + sideBox, _ := gtk.BoxNew(gtk.ORIENTATION_HORIZONTAL, 0) |
| 36 | + sideBox.SetHAlign(gtk.ALIGN_END) |
| 37 | + sc, _ := sideBox.GetStyleContext() |
| 38 | + sc.AddClass("sidestuff") |
| 39 | + |
| 40 | + otherIcons, _ := gtk.BoxNew(gtk.ORIENTATION_HORIZONTAL, 0) |
| 41 | + keyboardImage, _ := gtk.ImageNewFromIconName("input-keyboard-symbolic", gtk.ICON_SIZE_BUTTON) |
| 42 | + sc, _ = keyboardImage.GetStyleContext() |
| 43 | + sc.AddClass("keyboard") |
| 44 | + |
| 45 | + otherIcons.PackStart(keyboardImage, false, false, 0) |
| 46 | + sc, _ = otherIcons.GetStyleContext() |
| 47 | + sc.AddClass("other-icons-wrapper") |
| 48 | + |
| 49 | + statusBox, _ := gtk.BoxNew(gtk.ORIENTATION_HORIZONTAL, 0) |
| 50 | + |
| 51 | + volumeIcon, err := volumeHandler.GetAudioIcon() |
| 52 | + |
| 53 | + if err == nil { |
| 54 | + volumeImage, _ := gtk.ImageNewFromIconName(volumeIcon, gtk.ICON_SIZE_BUTTON) |
| 55 | + sc, _ = volumeImage.GetStyleContext() |
| 56 | + sc.AddClass("sound") |
| 57 | + |
| 58 | + statusBox.PackStart(volumeImage, false, false, 0) |
| 59 | + } |
| 60 | + |
| 61 | + networkIcon, err := networkManagerHandler.GetNetworkIcon() |
| 62 | + |
| 63 | + if err == nil { |
| 64 | + networkImage, _ := gtk.ImageNewFromIconName(networkIcon, gtk.ICON_SIZE_BUTTON) |
| 65 | + |
| 66 | + sc, _ = networkImage.GetStyleContext() |
| 67 | + sc.AddClass("network") |
| 68 | + |
| 69 | + statusBox.PackStart(networkImage, false, false, 0) |
| 70 | + } |
| 71 | + |
| 72 | + if batteryHandler.IsBattery() { |
| 73 | + batteryImage, _ := gtk.ImageNewFromIconName(batteryHandler.GetBatteryIcon(), gtk.ICON_SIZE_BUTTON) |
| 74 | + |
| 75 | + sc, _ = batteryImage.GetStyleContext() |
| 76 | + sc.AddClass("power") |
| 77 | + |
| 78 | + statusBox.PackStart(batteryImage, false, false, 0) |
| 79 | + } |
| 80 | + |
| 81 | + sc, _ = statusBox.GetStyleContext() |
| 82 | + sc.AddClass("status-icons-wrapper") |
| 83 | + |
| 84 | + curDayCal, currUTCTimeInString := getDateInfo() |
| 85 | + |
| 86 | + clock, _ := gtk.LabelNew(currUTCTimeInString) |
| 87 | + sc, _ = clock.GetStyleContext() |
| 88 | + sc.AddClass("clock-text") |
| 89 | + |
| 90 | + dayText, _ := gtk.LabelNew(curDayCal) |
| 91 | + sc, _ = dayText.GetStyleContext() |
| 92 | + sc.AddClass("day-text") |
| 93 | + |
| 94 | + glib.TimeoutAdd(uint(500), func() bool { |
| 95 | + // Get new date/time info. |
| 96 | + newDayCal, newTime := getDateInfo() |
| 97 | + |
| 98 | + // Update the labels. |
| 99 | + clock.SetText(newTime) |
| 100 | + dayText.SetText(newDayCal) |
| 101 | + |
| 102 | + // Return true to keep the timeout active. |
| 103 | + return true |
| 104 | + }) |
| 105 | + |
| 106 | + notificationButton, _ := gtk.ButtonNew() |
| 107 | + notificationBox, _ := gtk.BoxNew(gtk.ORIENTATION_HORIZONTAL, 0) |
| 108 | + sc, _ = notificationBox.GetStyleContext() |
| 109 | + sc.AddClass("notification-bell-wrapper") |
| 110 | + |
| 111 | + notificationImage, _ := gtk.ImageNewFromIconName("preferences-system-notifications-symbolic", gtk.ICON_SIZE_BUTTON) |
| 112 | + sc, _ = notificationImage.GetStyleContext() |
| 113 | + sc.AddClass("notification-bell") |
| 114 | + |
| 115 | + notificationBox.PackStart(notificationImage, false, false, 0) |
| 116 | + notificationButton.Add(notificationBox) |
| 117 | + |
| 118 | + sideBox.PackStart(otherIcons, false, false, 0) |
| 119 | + sideBox.PackStart(statusBox, false, false, 0) |
| 120 | + sideBox.PackStart(clock, false, false, 0) |
| 121 | + sideBox.PackStart(dayText, false, false, 0) |
| 122 | + sideBox.PackStart(notificationButton, false, false, 0) |
| 123 | + |
| 124 | + return sideBox |
| 125 | +} |
| 126 | + |
| 127 | +func createWorkspaces() *gtk.Box { |
| 128 | + box, _ := gtk.BoxNew(gtk.ORIENTATION_HORIZONTAL, 10) |
| 129 | + box.SetHAlign(gtk.ALIGN_START) |
| 130 | + sc, _ := box.GetStyleContext() |
| 131 | + sc.AddClass("workspaces") |
| 132 | + |
| 133 | + toplevels, err := foreignToplevel.ListToplevels() |
| 134 | + if err != nil { |
| 135 | + fmt.Println("Error getting toplevels:", err) |
| 136 | + return box |
| 137 | + } |
| 138 | + |
| 139 | + for _, k := range toplevels { |
| 140 | + imgButton, _ := gtk.ButtonNew() |
| 141 | + sc, _ := imgButton.GetStyleContext() |
| 142 | + sc.AddClass("app") |
| 143 | + |
| 144 | + img, _ := gtk.ImageNewFromIconName(k.AppID, gtk.ICON_SIZE_BUTTON) |
| 145 | + imgButton.Add(img) |
| 146 | + box.PackStart(imgButton, false, false, 0) |
| 147 | + } |
| 148 | + // Placeholder for dynamic window list |
| 149 | + imgButton1, _ := gtk.ButtonNew() |
| 150 | + sc, _ = imgButton1.GetStyleContext() |
| 151 | + sc.AddClass("app") |
| 152 | + img1, _ := gtk.ImageNewFromIconName("preferences-desktop", gtk.ICON_SIZE_BUTTON) |
| 153 | + imgButton1.Add(img1) |
| 154 | + box.PackStart(imgButton1, false, false, 0) |
| 155 | + |
| 156 | + return box |
| 157 | +} |
| 158 | + |
| 159 | +func createMainIcons() *gtk.Box { |
| 160 | + box, _ := gtk.BoxNew(gtk.ORIENTATION_HORIZONTAL, 10) |
| 161 | + box.SetHAlign(gtk.ALIGN_CENTER) |
| 162 | + |
| 163 | + desktopImage, _ := gtk.ImageNewFromIconName("preferences-system-windows-symbolic", gtk.ICON_SIZE_LARGE_TOOLBAR) |
| 164 | + searchImage, _ := gtk.ImageNewFromIconName("system-search-symbolic", gtk.ICON_SIZE_LARGE_TOOLBAR) |
| 165 | + customIcon, _ := gtk.ImageNewFromFile("images/pp.png") |
| 166 | + customButton, _ := gtk.ButtonNew() |
| 167 | + customButton.Add(customIcon) |
| 168 | + |
| 169 | + customButton.Connect("clicked", func() { |
| 170 | + createMainMenu().ShowAll() |
| 171 | + box.Hide() |
| 172 | + }) |
| 173 | + |
| 174 | + box.PackStart(desktopImage, false, false, 0) |
| 175 | + box.PackStart(customButton, false, false, 0) |
| 176 | + box.PackStart(searchImage, false, false, 0) |
| 177 | + |
| 178 | + return box |
| 179 | +} |
| 180 | + |
| 181 | +func createBar() *gtk.Window { |
| 182 | + win, _ := gtk.WindowNew(gtk.WINDOW_TOPLEVEL) |
| 183 | + win.SetTitle("Main Bar") |
| 184 | + win.SetDecorated(false) |
| 185 | + win.SetResizable(false) |
| 186 | + win.SetTypeHint(gdk.WINDOW_TYPE_HINT_DOCK) |
| 187 | + |
| 188 | + layershell.InitForWindow(win) |
| 189 | + layershell.SetNamespace(win, "miracleos") |
| 190 | + layershell.SetAnchor(win, layershell.LAYER_SHELL_EDGE_LEFT, true) |
| 191 | + layershell.SetAnchor(win, layershell.LAYER_SHELL_EDGE_BOTTOM, true) |
| 192 | + layershell.SetAnchor(win, layershell.LAYER_SHELL_EDGE_RIGHT, true) |
| 193 | + |
| 194 | + layershell.SetLayer(win, layershell.LAYER_SHELL_LAYER_TOP) |
| 195 | + layershell.SetMargin(win, layershell.LAYER_SHELL_EDGE_TOP, 0) |
| 196 | + layershell.SetMargin(win, layershell.LAYER_SHELL_EDGE_LEFT, 0) |
| 197 | + layershell.SetMargin(win, layershell.LAYER_SHELL_EDGE_RIGHT, 0) |
| 198 | + |
| 199 | + layershell.SetExclusiveZone(win, 75) |
| 200 | + layershell.SetKeyboardMode(win, layershell.LAYER_SHELL_KEYBOARD_MODE_NONE) |
| 201 | + disp, _ := gdk.DisplayGetDefault() |
| 202 | + mon, _ := disp.GetMonitor(0) |
| 203 | + layershell.SetMonitor(win, mon) |
| 204 | + |
| 205 | + box, _ := gtk.BoxNew(gtk.ORIENTATION_HORIZONTAL, 10) |
| 206 | + sc, _ := box.GetStyleContext() |
| 207 | + sc.AddClass("bar") |
| 208 | + box.PackStart(createWorkspaces(), false, false, 0) |
| 209 | + box.SetCenterWidget(createMainIcons()) |
| 210 | + box.PackEnd(createSidestuff(), false, false, 0) |
| 211 | + win.Add(box) |
| 212 | + return win |
| 213 | +} |
0 commit comments