From 95418a6bafa0cbd206b88db11e12e07ac573f1f1 Mon Sep 17 00:00:00 2001 From: S4NKALP Date: Thu, 8 Aug 2024 21:29:06 +0545 Subject: [PATCH] update --- config/ags/main.ts | 4 +- config/ags/modules/audio.ts | 43 +++-- config/ags/modules/bar.ts | 29 +-- config/ags/modules/calendar.ts | 162 ++++++++++++++++ config/ags/modules/cliphist.ts | 13 ++ config/ags/modules/misc/calendar_layout.ts | 84 +++++++++ config/ags/modules/sideleft/cliphist.ts | 204 --------------------- config/ags/modules/sideleft/main.ts | 14 -- config/ags/scripts/cliphist.sh | 6 + config/ags/style.css | 39 ++++ config/hypr/UserConfigs/UserKeybinds.conf | 2 +- 11 files changed, 356 insertions(+), 244 deletions(-) create mode 100644 config/ags/modules/calendar.ts create mode 100644 config/ags/modules/misc/calendar_layout.ts delete mode 100644 config/ags/modules/sideleft/cliphist.ts diff --git a/config/ags/main.ts b/config/ags/main.ts index cf0f1944..0804b1bd 100644 --- a/config/ags/main.ts +++ b/config/ags/main.ts @@ -14,6 +14,7 @@ import Window from "types/widgets/window"; import { popups } from "modules/popups.ts"; import { start_battery_warning_service } from "services/battery_warning.ts"; import { audio_popup } from "./modules/audio.ts" +import { calendar } from "modules/calendar.ts"; import Gtk from "gi://Gtk?version=3.0"; const GLib = imports.gi.GLib; @@ -36,7 +37,8 @@ const Windows = () => [ cheatsheet, sideleft, forMonitors(popups), - audio_popup + audio_popup, + calendar ]; App.config({ diff --git a/config/ags/modules/audio.ts b/config/ags/modules/audio.ts index 613ab20b..d2c73125 100644 --- a/config/ags/modules/audio.ts +++ b/config/ags/modules/audio.ts @@ -77,12 +77,21 @@ const speaker = (stream: Stream, icons: [number, string][], off: string) => vertical: true, attribute: { stream: stream }, children: [ - Widget.Label({ - label: stream.description, - hpack: "start", - xalign: 0, - truncate: "middle", - tooltip_text: stream.name + Widget.Box({ + children: [ + Widget.Label({ + label: stream.description, + xalign: 0, + truncate: "end", + hexpand: true, + tooltip_text: stream.name, + css: "margin-right: 4px;" + }), + Widget.Label({ + label: stream.bind("volume").as((v) => `${Math.round(v * 100)}%`), + xalign: 1 + }) + ] }), Widget.Box({ children: [ @@ -109,13 +118,23 @@ const app = (stream: Stream) => { vertical: true, attribute: { stream: stream }, children: [ - Widget.Label({ - label: label, - hpack: "start", - xalign: 0, - truncate: "middle", - tooltip_text: label + Widget.Box({ + children: [ + Widget.Label({ + label: label, + xalign: 0, + truncate: "end", + hexpand: true, + tooltip_text: label, + css: "margin-right: 4px;" + }), + Widget.Label({ + label: stream.bind("volume").as((v) => `${Math.round(v * 100)}%`), + xalign: 1 + }) + ] }), + Widget.Box({ children: [ Widget.Button({ diff --git a/config/ags/modules/bar.ts b/config/ags/modules/bar.ts index ac23f59e..da7fb3c4 100644 --- a/config/ags/modules/bar.ts +++ b/config/ags/modules/bar.ts @@ -154,19 +154,24 @@ function Workspaces() { } function Clock() { - return Widget.Box({ - orientation: Gtk.Orientation.VERTICAL, + return Widget.Button({ class_name: "clock", - children: [ - Widget.Label({ - class_name: "time", - label: time.bind() - }), - Widget.Label({ - class_name: "date", - label: date.bind() - }) - ] + child: Widget.Box({ + orientation: Gtk.Orientation.VERTICAL, + children: [ + Widget.Label({ + class_name: "time", + label: time.bind() + }), + Widget.Label({ + class_name: "date", + label: date.bind() + }) + ] + }), + on_clicked: (self) => { + App.toggleWindow("calendar"); + } }); } diff --git a/config/ags/modules/calendar.ts b/config/ags/modules/calendar.ts new file mode 100644 index 00000000..7187858d --- /dev/null +++ b/config/ags/modules/calendar.ts @@ -0,0 +1,162 @@ +const { Box, Button, Label } = Widget; +import { MaterialIcon } from "icons.ts"; + +import { get_calendar_layout } from "./misc/calendar_layout.js"; +import popupwindow from "./misc/popupwindow.ts"; + +const WINDOW_NAME: string = "calendar"; + +type CalendarDay = { + day: string; + today: number; +}; + +let calendar_json: CalendarDay[][] = get_calendar_layout(undefined, true); +let month_shift: number = 0; + +function get_date_in_x_months_time(x: number): Date { + const currentDate: Date = new Date(); + let targetMonth: number = currentDate.getMonth() + x; + let targetYear: number = currentDate.getFullYear(); + + targetYear += Math.floor(targetMonth / 12); + targetMonth = ((targetMonth % 12) + 12) % 12; + + const targetDate: Date = new Date(targetYear, targetMonth, 1); + + return targetDate; +} + +const week_days: CalendarDay[] = [ + { day: "Mo", today: 0 }, + { day: "Tu", today: 0 }, + { day: "We", today: 0 }, + { day: "Th", today: 0 }, + { day: "Fr", today: 0 }, + { day: "Sa", today: 0 }, + { day: "Su", today: 0 } +]; + +const calendar_day = (day: string, today: number) => + Widget.Button({ + class_name: `calendar_button ${today == 1 ? "today" : today == -1 ? "othermonth" : ""}`, + child: Widget.Overlay({ + child: Box({ + class_name: "overlay_box" + }), + overlays: [ + Label({ + hpack: "center", + label: String(day) + }) + ] + }) + }); + +const calendar_widget = () => { + const month_year = Widget.Button({ + class_name: "calendar_monthyear", + on_clicked: (): void => shift_x_months(0), + setup: (button: any): void => { + button.label = `${new Date().toLocaleString("default", { month: "long" })} ${new Date().getFullYear()}`; + } + }); + + const add_children = (box: ReturnType, calendar_json: CalendarDay[][]): void => { + const children = box.get_children(); + for (let i = 0; i < children.length; i++) { + const child: any = children[i]; + child.destroy(); + } + box.children = calendar_json.map((row, i) => + Widget.Box({ + spacing: 5, + children: row.map((day, i) => calendar_day(day.day, day.today)) + }) + ); + }; + + function shift_x_months(x: number): void { + if (x == 0) month_shift = 0; + else month_shift += x; + + const new_date: Date = month_shift == 0 ? new Date() : get_date_in_x_months_time(month_shift); + + calendar_json = get_calendar_layout(new_date, month_shift == 0); + month_year.label = `${month_shift == 0 ? "" : "• "}${new_date.toLocaleString("default", { + month: "long" + })} ${new_date.getFullYear()}`; + add_children(calendar_days, calendar_json); + } + + const calendar_header = Widget.Box({ + class_name: "calendar_header", + spacing: 5, + setup: (box: any): void => { + box.pack_start(month_year, false, false, 0); + box.pack_end( + Widget.Box({ + spacing: 5, + children: [ + Button({ + class_name: "calendar_month_shift standard_icon_button", + on_clicked: () => shift_x_months(-1), + child: MaterialIcon("chevron_left", "20px") + }), + Button({ + class_name: "calendar_month_shift standard_icon_button", + on_clicked: () => shift_x_months(1), + child: MaterialIcon("chevron_right", "20px") + }) + ] + }), + false, + false, + 0 + ); + } + }); + + const calendar_days = Widget.Box({ + hexpand: true, + vertical: true, + spacing: 5, + setup: (box: any): void => { + add_children(box, calendar_json); + } + }); + + return Widget.EventBox({ + on_scroll_up: (): void => shift_x_months(-1), + on_scroll_down: (): void => shift_x_months(1), + child: Widget.Box({ + hpack: "center", + class_name: "calendar_widget", + children: [ + Widget.Box({ + hexpand: true, + vertical: true, + spacing: 5, + children: [ + calendar_header, + Widget.Box({ + homogeneous: true, + spacing: 5, + children: week_days.map((day, i) => calendar_day(day.day, day.today)) + }), + calendar_days + ] + }) + ] + }) + }); +}; + +export const calendar = popupwindow({ + name: WINDOW_NAME, + class_name: "calendar", + visible: false, + keymode: "exclusive", + child: calendar_widget(), + anchor: ["top", "right"] +}); diff --git a/config/ags/modules/cliphist.ts b/config/ags/modules/cliphist.ts index b70cb612..09f37353 100644 --- a/config/ags/modules/cliphist.ts +++ b/config/ags/modules/cliphist.ts @@ -182,6 +182,19 @@ function ClipHistWidget({ width = 500, height = 500, spacing = 12 }) { on_change: ({ text }) => { const searchText = (text ?? "").toLowerCase(); + + if (searchText === "/clear") { + list.children.forEach((item) => { + item.destroy(); + }); + clipHistItems = []; + Utils.execAsync(`${App.configDir}/scripts/cliphist.sh --clear`).catch(print); + setTimeout(() => { + entry.text = ""; + }, 300); + return; + } + list.children.forEach((item) => { item.visible = item.attribute.content.toLowerCase().includes(searchText); }); diff --git a/config/ags/modules/misc/calendar_layout.ts b/config/ags/modules/misc/calendar_layout.ts new file mode 100644 index 00000000..85378270 --- /dev/null +++ b/config/ags/modules/misc/calendar_layout.ts @@ -0,0 +1,84 @@ +function check_leap_year(year) { + return ( + year % 400 == 0 || + (year % 4 == 0 && year % 100 != 0)); +} + +function month_day(month, year) { + const leap_year = check_leap_year(year); + if ((month <= 7 && month % 2 == 1) || (month >= 8 && month % 2 == 0)) return 31; + if (month == 2 && leap_year) return 29; + if (month == 2 && !leap_year) return 28; + return 30; +} + +function next_month_days(month, year) { + const leap_year = check_leap_year(year); + if (month == 1 && leap_year) return 29; + if (month == 1 && !leap_year) return 28; + if (month == 12) return 31; + if ((month <= 7 && month % 2 == 1) || (month >= 8 && month % 2 == 0)) return 30; + return 31; +} + +function prev_month_days(month, year) { + const leap_year = check_leap_year(year); + if (month == 3 && leap_year) return 29; + if (month == 3 && !leap_year) return 28; + if (month == 1) return 31; + if ((month <= 7 && month % 2 == 1) || (month >= 8 && month % 2 == 0)) return 30; + return 31; +} + +export function get_calendar_layout(date_object, highlight) { + if (!date_object) date_object = new Date(); + const weekday = (date_object.getDay() + 6) % 7; + const day = date_object.getDate(); + const month = date_object.getMonth() + 1; + const year = date_object.getFullYear(); + const weekday_of_month_first = (weekday + 35 - (day - 1)) % 7; + const days_in_month = month_day(month, year); + const days_in_next_month = next_month_days(month, year); + const days_in_prev_month = prev_month_days(month, year); + + // Fill + var month_diff = (weekday_of_month_first == 0 ? 0 : -1); + var to_fill, dim; + if(weekday_of_month_first == 0) { + to_fill = 1; + dim = days_in_month; + } + else { + to_fill = (days_in_prev_month - (weekday_of_month_first - 1)); + dim = days_in_prev_month; + } + var calendar = [...Array(6)].map(() => Array(7)); + var i = 0, j = 0; + while (i < 6 && j < 7) { + calendar[i][j] = { + "day": to_fill, + "today": ((to_fill == day && month_diff == 0 && highlight) ? 1 : ( + month_diff == 0 ? 0 : + -1 + )) + }; + // Increment + to_fill++; + if (to_fill > dim) { // Next month? + month_diff++; + if (month_diff == 0) + dim = days_in_month; + else if (month_diff == 1) + dim = days_in_next_month; + to_fill = 1; + } + // Next tile + j++; + if (j == 7) { + j = 0; + i++; + } + + } + return calendar; +} diff --git a/config/ags/modules/sideleft/cliphist.ts b/config/ags/modules/sideleft/cliphist.ts deleted file mode 100644 index 1ae1c591..00000000 --- a/config/ags/modules/sideleft/cliphist.ts +++ /dev/null @@ -1,204 +0,0 @@ -import Gtk from "gi://Gtk?version=3.0"; -const { Gio } = imports.gi; -const GLib = imports.gi.GLib; -import Box from "types/widgets/box"; -import { WINDOW_NAME } from "modules/sideleft/main"; - -async function getClipboardHistory() { - try { - const process = new Gio.Subprocess({ - argv: ['bash', '-c', `${App.configDir}/scripts/cliphist.sh --get`], - flags: Gio.SubprocessFlags.STDOUT_PIPE | Gio.SubprocessFlags.STDERR_PIPE - }); - - process.init(null); - const [success, stdout] = await process.communicate_utf8(null, null); - if (!success) return []; - - return stdout.split("\n").filter(line => line.trim() !== ""); - } catch (error) { - return []; - } -} - -async function clearClipboardHistory() { - try { - const process = new Gio.Subprocess({ - argv: ['bash', '-c', 'cliphist wipe'], - flags: Gio.SubprocessFlags.STDOUT_PIPE | Gio.SubprocessFlags.STDERR_PIPE - }); - - process.init(null); - await process.communicate_utf8(null, null); - } catch (error) { - } -} - -async function showImagePreview(id, format, width, height) { - try { - const file = await Utils.execAsync(`${App.configDir}/scripts/cliphist.sh --save-by-id ${id}`); - const _widthPx = Number(width); - const heightPx = Number(height); - const maxWidth = 400; - const widthPx = (_widthPx / heightPx) * 150; - - let css = `background-image: url("${file}");`; - - if (widthPx > maxWidth) { - const newHeightPx = (150 / widthPx) * maxWidth; - css += `min-height: ${newHeightPx}px; min-width: ${maxWidth}px;`; - } else { - css += `min-height: 150px; min-width: ${widthPx}px;`; - } - - return css; - } catch (error) { - return ''; - } -} - -function ClipHistItem(entry) { - const [id, ..._content] = entry.split("\t"); - const content = _content.join(" ").trim(); - const matches = content.match(/\[\[ binary data (\d+) (KiB|MiB) (\w+) (\d+)x(\d+) \]\]/); - let _show_image = false; - - let clickCount = 0; - const button = Widget.Button({ - class_name: "clip_container", - child: Widget.Box({ - children: [ - Widget.Label({ label: id, class_name: "clip_id", xalign: 0, vpack: "center" }), - Widget.Label({ label: "・", class_name: "dot_divider", xalign: 0, vpack: "center" }), - Widget.Label({ label: content, class_name: "clip_label", xalign: 0, vpack: "center", truncate: "end" }) - ] - }) - }); - - button.connect("clicked", async () => { - try { - clickCount++; - if (clickCount === 2) { - App.closeWindow(WINDOW_NAME); - await Utils.execAsync(`${App.configDir}/scripts/cliphist.sh --copy-by-id ${id}`); - clickCount = 0; - } - } catch (error) { - } - }); - - button.connect("focus-out-event", () => { - clickCount = 0; - }); - - if (matches) { - const format = matches[3]; - const width = matches[4]; - const height = matches[5]; - if (format === "png") { - button.toggleClassName("with_image", true); - button.connect("clicked", async () => { - if (!_show_image) { - const css = await showImagePreview(id, format, width, height); - const box = button.child; - box.children[2].destroy(); - const icon = Widget.Box({ - vpack: "center", - css: css, - class_name: "preview" - }); - box.children = [...box.children, icon]; - _show_image = true; - } - }); - } - } - - return Widget.Box({ - attribute: { content: content }, - orientation: Gtk.Orientation.VERTICAL, - children: [ - button, - Widget.Separator({ - class_name: "clip_divider", - orientation: Gtk.Orientation.HORIZONTAL - }) - ] - }); -} - -export const cliphist = () => { - const list = Widget.Box({ vertical: true, vexpand: true }); - const loadingIndicator = Widget.Label({ - label: "Loading...", - class_name: "loading" - }); - list.children = [loadingIndicator]; - - async function repopulate() { - try { - const entries = await getClipboardHistory(); - list.children = []; - - if (entries.length > 0) { - list.children = entries.map(ClipHistItem); - } - } catch (error) { - list.children = [Widget.Label({ - label: "Error loading history", - class_name: "error_loading" - })]; - } - } - - repopulate(); - - const entry = Widget.Entry({ - hexpand: true, - class_name: "cliphistory_entry", - placeholder_text: "Search", - on_change: async ({ text }) => { - const searchText = text.toLowerCase(); - - if (searchText.trim() === '/clear') { - await clearClipboardHistory(); - await repopulate(); - entry.text = ''; - } else { - list.children.forEach((item) => { - item.visible = item.attribute.content.toLowerCase().includes(searchText); - }); - } - } - }); - - const searchBox = Widget.Box({ - orientation: Gtk.Orientation.HORIZONTAL, - children: [entry] - }); - - return Widget.Box({ - vertical: true, - class_name: "cliphistory_box", - margin_top: 14, - margin_right: 14, - children: [ - searchBox, - Widget.Separator(), - Widget.Scrollable({ - hscroll: "never", - child: list, - vexpand: true - }) - ], - setup: (self) => - self.hook(App, (_, windowName, visible) => { - if (windowName !== WINDOW_NAME) return; - - if (visible) { - repopulate(); - entry.text = ""; - } - }) - }); -}; diff --git a/config/ags/modules/sideleft/main.ts b/config/ags/modules/sideleft/main.ts index d911950a..d77444df 100644 --- a/config/ags/modules/sideleft/main.ts +++ b/config/ags/modules/sideleft/main.ts @@ -4,7 +4,6 @@ import { MaterialIcon } from "icons.ts"; import { WeatherBox } from "./weather.ts"; import { Media } from "./players.ts"; import { Applauncher } from "./applauncher.ts"; -import { cliphist } from "./cliphist.ts"; import { geminiPage } from "./gemini.ts"; export const WINDOW_NAME = "sideleft"; @@ -24,12 +23,6 @@ export function toggleMediaWindow() { shown.setValue("media"); } } -export function toggleClipsWindow() { - if (shown.value == "clips" && sideleft.visible) App.closeWindow(WINDOW_NAME); else { - App.openWindow(WINDOW_NAME); - shown.setValue("clips"); - } -} export function toggleChatsWindow() { if (shown.value == "gemini" && sideleft.visible) App.closeWindow(WINDOW_NAME); else { App.openWindow(WINDOW_NAME); @@ -37,7 +30,6 @@ export function toggleChatsWindow() { } } -globalThis.toggleClipsWindow = toggleClipsWindow; globalThis.toggleChatsWindow = toggleChatsWindow; globalThis.toggleMediaWindow = toggleMediaWindow; globalThis.toggleAppsWindow = toggleAppsWindow; @@ -90,7 +82,6 @@ function Navigation() { weather: WeatherBox(), media: Media(), apps: Applauncher(), - clips: cliphist(), gemini: geminiPage }, hexpand: true, @@ -119,11 +110,6 @@ function Navigation() { page: "apps", label: "Apps", icon: "search" - }), - Button({ - page: "clips", - label: "Clipboard", - icon: "content_paste_search", }), Button({ page: "gemini", diff --git a/config/ags/scripts/cliphist.sh b/config/ags/scripts/cliphist.sh index cdf6d547..feb9ac5d 100755 --- a/config/ags/scripts/cliphist.sh +++ b/config/ags/scripts/cliphist.sh @@ -9,6 +9,10 @@ copy_by_id() { cliphist decode $id | wl-copy } +clear(){ + cliphist wipe +} + save_cache_file() { id=$1 @@ -34,4 +38,6 @@ elif [[ "$1" == "--save-by-id" ]]; then { save_cache_file "$2"; } elif [[ "$1" == "--clear-cache" ]]; then clear_tmp +elif [[ "$1" == "--clear" ]]; then + clear fi diff --git a/config/ags/style.css b/config/ags/style.css index d3fafa83..185b5ec9 100644 --- a/config/ags/style.css +++ b/config/ags/style.css @@ -1716,6 +1716,45 @@ switch:checked:active slider { background-color: @primary; } +/* ----------------------------------------------------- + * Calendar + * ----------------------------------------------------- */ + +.calendar_widget { + padding: 4px; + margin-top: 14px; + margin-right: 14px; + background-color: @background; + border-radius: 20px; + border: 1px solid @outlineVariant; +} + +.calendar_button .overlay_box { + min-width: 22px; + min-height: 32px; +} + +.calendar_button label { + font-size: 14px; + color: @onSurface; +} + +.calendar_button.today { + background-color: @primary; +} + +.calendar_button.today label { + color: @onPrimary; +} + +.calendar_button.othermonth label { + color: mix(@onSurfaceVariant, transparent, 0.3); +} + +.calendar_monthyear { + color: @foreground; +} + /* ----------------------------------------------------- * Material * ----------------------------------------------------- */ diff --git a/config/hypr/UserConfigs/UserKeybinds.conf b/config/hypr/UserConfigs/UserKeybinds.conf index 7025a7f1..57e89ad5 100644 --- a/config/hypr/UserConfigs/UserKeybinds.conf +++ b/config/hypr/UserConfigs/UserKeybinds.conf @@ -45,7 +45,7 @@ bind = $mainMod SHIFT, Z, exec, ags -r "toggleMediaWindow()" #! @description: bind = ALT, R, exec, ags -t sidebar #! @description: "rightbar"; @tools; bind = ALT, L, exec, ags -t sideleft #! @description: "leftbar"; @tools; bind = $mainMod, PERIOD, exec, ags -r "OpenEmojiPicker()" #! @description: "Emoji Picker"; @tools; -bind = SHIFT ALT, C, exec, ags -r "toggleClipsWindow()" #! @description: "Clipboard history"; @tools; +bind = SHIFT ALT, C, exec, ags -t "cliphist" #! @description: "Clipboard history"; @tools; bind = $mainMod, W, exec, ags -r "OpenSettings('wallpaper')" #! @description: "Change wallpaper"; @tools; bind = $mainMod SHIFT, S, exec, ags -r 'OpenSettings()' #! @description: "Setting"; @tools; bind = ALT, F1, exec, ags -t cheatsheet #! @description: "List of keybindings"; @tools;