forked from Shinonome/dots-hyprland
ags: sync
- workspace indicator: no more goofy notch, now cairo-drawn to ensure accuracy and maintain animations - made notification icons a bit smaller
This commit is contained in:
@@ -1,98 +1,165 @@
|
||||
const { GLib, Gdk, Gtk } = imports.gi;
|
||||
const Lang = imports.lang;
|
||||
const Cairo = imports.cairo;
|
||||
const Pango = imports.gi.Pango;
|
||||
const PangoCairo = imports.gi.PangoCairo;
|
||||
import { App, Service, Utils, Widget } from '../../imports.js';
|
||||
const { Box, DrawingArea, EventBox } = Widget;
|
||||
import Hyprland from 'resource:///com/github/Aylur/ags/service/hyprland.js';
|
||||
|
||||
const WORKSPACE_SIDE_PAD = 0.546; // rem
|
||||
const NUM_OF_WORKSPACES = 10;
|
||||
let lastWorkspace = 0;
|
||||
const dummyWs = Box({ className: 'bar-ws' }); // Not shown. Only for getting size props
|
||||
const dummyActiveWs = Box({ className: 'bar-ws bar-ws-active' }); // Not shown. Only for getting size props
|
||||
const dummyOccupiedWs = Box({ className: 'bar-ws bar-ws-occupied' }); // Not shown. Only for getting size props
|
||||
|
||||
const activeWorkspaceIndicator = Widget.Box({
|
||||
css: `
|
||||
padding: 0rem ${WORKSPACE_SIDE_PAD}rem;
|
||||
`,
|
||||
children: [
|
||||
Widget.Box({
|
||||
vpack: 'center',
|
||||
hpack: 'start',
|
||||
className: 'bar-ws-active-box',
|
||||
setup: (self) => self.hook(Hyprland.active.workspace, (box) => {
|
||||
const ws = Hyprland.active.workspace.id;
|
||||
box.setCss(`
|
||||
margin-left: ${1.774 * (ws - 1) + 0.068}rem;
|
||||
`);
|
||||
lastWorkspace = ws;
|
||||
}),
|
||||
children: [
|
||||
Widget.Label({
|
||||
vpack: 'center',
|
||||
className: 'bar-ws-active',
|
||||
label: `•`,
|
||||
})
|
||||
]
|
||||
})
|
||||
]
|
||||
});
|
||||
// Font size = workspace id
|
||||
const WorkspaceContents = (count = 10) => {
|
||||
return DrawingArea({
|
||||
properties: [
|
||||
['workspaceMask', 0],
|
||||
],
|
||||
css: `transition: 500ms cubic-bezier(0.1, 1, 0, 1);`,
|
||||
setup: (area) => area
|
||||
.hook(Hyprland.active.workspace, (area) =>
|
||||
area.setCss(`font-size: ${Hyprland.active.workspace.id}px;`)
|
||||
)
|
||||
.hook(Hyprland, (area) => {
|
||||
const workspaces = Hyprland.workspaces;
|
||||
let workspaceMask = 0;
|
||||
for (let i = 0; i < workspaces.length; i++) {
|
||||
const ws = workspaces[i];
|
||||
if (ws.id < 0) continue; // Ignore scratchpads
|
||||
if (ws.id > count) return; // Not rendered
|
||||
if (workspaces[i].windows > 0) {
|
||||
workspaceMask |= (1 << ws.id);
|
||||
}
|
||||
}
|
||||
area._workspaceMask = workspaceMask;
|
||||
}, 'notify::workspaces')
|
||||
.on('draw', Lang.bind(area, (area, cr) => {
|
||||
const allocation = area.get_allocation();
|
||||
const { width, height } = allocation;
|
||||
|
||||
export const ModuleWorkspaces = () => Widget.EventBox({
|
||||
onScrollUp: () => Utils.execAsync(['bash', '-c', 'hyprctl dispatch workspace -1 &']),
|
||||
onScrollDown: () => Utils.execAsync(['bash', '-c', 'hyprctl dispatch workspace +1 &']),
|
||||
const workspaceStyleContext = dummyWs.get_style_context();
|
||||
const workspaceDiameter = workspaceStyleContext.get_property('min-width', Gtk.StateFlags.NORMAL);
|
||||
const workspaceRadius = workspaceDiameter / 2;
|
||||
const workspaceFontSize = workspaceStyleContext.get_property('font-size', Gtk.StateFlags.NORMAL) / 4 * 3;
|
||||
const workspaceFontFamily = workspaceStyleContext.get_property('font-family', Gtk.StateFlags.NORMAL);
|
||||
const wsbg = workspaceStyleContext.get_property('background-color', Gtk.StateFlags.NORMAL);
|
||||
const wsfg = workspaceStyleContext.get_property('color', Gtk.StateFlags.NORMAL);
|
||||
|
||||
const occupiedWorkspaceStyleContext = dummyOccupiedWs.get_style_context();
|
||||
const occupiedbg = occupiedWorkspaceStyleContext.get_property('background-color', Gtk.StateFlags.NORMAL);
|
||||
const occupiedfg = occupiedWorkspaceStyleContext.get_property('color', Gtk.StateFlags.NORMAL);
|
||||
|
||||
const activeWorkspaceStyleContext = dummyActiveWs.get_style_context();
|
||||
const activebg = activeWorkspaceStyleContext.get_property('background-color', Gtk.StateFlags.NORMAL);
|
||||
const activefg = activeWorkspaceStyleContext.get_property('color', Gtk.StateFlags.NORMAL);
|
||||
area.set_size_request(workspaceDiameter * count, -1);
|
||||
const widgetStyleContext = area.get_style_context();
|
||||
const activeWs = widgetStyleContext.get_property('font-size', Gtk.StateFlags.NORMAL);
|
||||
|
||||
const activeWsCenterX = -(workspaceDiameter / 2) + (workspaceDiameter * activeWs);
|
||||
const activeWsCenterY = height / 2;
|
||||
|
||||
// Font
|
||||
const layout = PangoCairo.create_layout(cr);
|
||||
const fontDesc = Pango.font_description_from_string(`${workspaceFontFamily[0]} ${workspaceFontSize}`);
|
||||
layout.set_font_description(fontDesc);
|
||||
// Get kinda min radius for number indicators
|
||||
layout.set_text("0".repeat(count.toString().length), -1);
|
||||
const [layoutWidth, layoutHeight] = layout.get_pixel_size();
|
||||
const indicatorRadius = Math.max(layoutWidth, layoutHeight) / 2 * 1.2; // a bit smaller than sqrt(2)*radius
|
||||
const indicatorGap = workspaceRadius - indicatorRadius;
|
||||
|
||||
// Draw workspace numbers
|
||||
for (let i = 1; i <= count; i++) {
|
||||
if (area._workspaceMask & (1 << i)) {
|
||||
// Draw bg highlight
|
||||
cr.setSourceRGBA(occupiedbg.red, occupiedbg.green, occupiedbg.blue, occupiedbg.alpha);
|
||||
const wsCenterX = -(workspaceRadius) + (workspaceDiameter * i);
|
||||
const wsCenterY = height / 2;
|
||||
if (!(area._workspaceMask & (1 << (i - 1)))) { // Left
|
||||
cr.arc(wsCenterX, wsCenterY, workspaceRadius, 0.5 * Math.PI, 1.5 * Math.PI);
|
||||
cr.fill();
|
||||
}
|
||||
else {
|
||||
cr.rectangle(wsCenterX - workspaceRadius , wsCenterY - workspaceRadius, workspaceRadius , workspaceRadius * 2)
|
||||
cr.fill();
|
||||
}
|
||||
if (!(area._workspaceMask & (1 << (i + 1)))) { // Right
|
||||
cr.arc(wsCenterX, wsCenterY, workspaceRadius, -0.5 * Math.PI, 0.5 * Math.PI);
|
||||
cr.fill();
|
||||
}
|
||||
else {
|
||||
cr.rectangle(wsCenterX, wsCenterY - workspaceRadius, workspaceRadius , workspaceRadius * 2)
|
||||
cr.fill();
|
||||
}
|
||||
|
||||
// Set color for text
|
||||
cr.setSourceRGBA(occupiedfg.red, occupiedfg.green, occupiedfg.blue, occupiedfg.alpha);
|
||||
}
|
||||
else
|
||||
cr.setSourceRGBA(wsfg.red, wsfg.green, wsfg.blue, wsfg.alpha);
|
||||
layout.set_text(`${i}`, -1);
|
||||
const [layoutWidth, layoutHeight] = layout.get_pixel_size();
|
||||
const x = -workspaceRadius + (workspaceDiameter * i) - (layoutWidth / 2);
|
||||
const y = (height - layoutHeight) / 2;
|
||||
cr.moveTo(x, y);
|
||||
// cr.showText(text);
|
||||
PangoCairo.show_layout(cr, layout);
|
||||
cr.stroke();
|
||||
}
|
||||
|
||||
// Draw active ws
|
||||
// base
|
||||
cr.setSourceRGBA(activebg.red, activebg.green, activebg.blue, activebg.alpha);
|
||||
cr.arc(activeWsCenterX, activeWsCenterY, indicatorRadius, 0, 2 * Math.PI);
|
||||
cr.fill();
|
||||
// inner decor
|
||||
cr.setSourceRGBA(activefg.red, activefg.green, activefg.blue, activefg.alpha);
|
||||
cr.arc(activeWsCenterX, activeWsCenterY, indicatorRadius * 0.19, 0, 2 * Math.PI);
|
||||
cr.fill();
|
||||
}))
|
||||
,
|
||||
})
|
||||
}
|
||||
|
||||
export default () => EventBox({
|
||||
onScrollUp: () => Hyprland.sendMessage(`dispatch workspace -1`),
|
||||
onScrollDown: () => Hyprland.sendMessage(`dispatch workspace +1`),
|
||||
onMiddleClickRelease: () => App.toggleWindow('overview'),
|
||||
onSecondaryClickRelease: () => App.toggleWindow('osk'),
|
||||
child: Widget.Box({
|
||||
properties: [
|
||||
['clicked', false],
|
||||
],
|
||||
child: Box({
|
||||
homogeneous: true,
|
||||
className: 'bar-ws-width',
|
||||
children: [
|
||||
Widget.Overlay({
|
||||
passThrough: true,
|
||||
child: Widget.Box({
|
||||
homogeneous: true,
|
||||
className: 'bar-group-center',
|
||||
children: [Widget.Box({
|
||||
className: 'bar-group-standalone bar-group-pad',
|
||||
})]
|
||||
}),
|
||||
overlays: [
|
||||
Widget.Overlay({
|
||||
setup: (self) => self.set_overlay_pass_through(self.get_children()[1], true),
|
||||
child: Widget.Box({
|
||||
hpack: 'center',
|
||||
css: `
|
||||
padding: 0rem ${WORKSPACE_SIDE_PAD}rem;
|
||||
`,
|
||||
// homogeneous: true,
|
||||
children: Array.from({ length: NUM_OF_WORKSPACES }, (_, i) => i + 1).map(i => Widget.Button({
|
||||
onPrimaryClick: () => Utils.execAsync(['bash', '-c', `hyprctl dispatch workspace ${i} &`]).catch(print),
|
||||
child: Widget.Label({
|
||||
vpack: 'center',
|
||||
label: `${i}`,
|
||||
className: 'bar-ws txt',
|
||||
}),
|
||||
})),
|
||||
setup: (self) => self.hook(Hyprland, (box) => {
|
||||
// console.log('update');
|
||||
const kids = box.children;
|
||||
for (let i = 0; i < kids.length; i++) {
|
||||
const child = kids[i];
|
||||
child.child.toggleClassName('bar-ws-occupied', false);
|
||||
child.child.toggleClassName('bar-ws-occupied-left', false);
|
||||
child.child.toggleClassName('bar-ws-occupied-right', false);
|
||||
child.child.toggleClassName('bar-ws-occupied-left-right', false);
|
||||
}
|
||||
|
||||
const occupied = Array.from({ length: NUM_OF_WORKSPACES }, (_, i) => Hyprland.getWorkspace(i + 1)?.windows > 0);
|
||||
for (let i = 0; i < occupied.length; i++) {
|
||||
if (!occupied[i]) continue;
|
||||
const child = kids[i];
|
||||
child.child.toggleClassName(`bar-ws-occupied${!occupied[i - 1] ? '-left' : ''}${!occupied[i + 1] ? '-right' : ''}`, true);
|
||||
}
|
||||
}, 'notify::workspaces'),
|
||||
}),
|
||||
overlays: [
|
||||
activeWorkspaceIndicator,
|
||||
]
|
||||
})
|
||||
],
|
||||
})
|
||||
]
|
||||
})
|
||||
});
|
||||
className: 'bar-group-margin',
|
||||
children: [Box({
|
||||
className: 'bar-group bar-group-standalone bar-group-pad',
|
||||
css: 'min-width: 2px;',
|
||||
children: [
|
||||
WorkspaceContents(10),
|
||||
]
|
||||
})]
|
||||
}),
|
||||
setup: (self) => {
|
||||
self.add_events(Gdk.EventMask.POINTER_MOTION_MASK);
|
||||
self.on('motion-notify-event', (self, event) => {
|
||||
if (!self._clicked) return;
|
||||
const [_, cursorX, cursorY] = event.get_coords();
|
||||
const widgetWidth = self.get_allocation().width;
|
||||
const wsId = Math.ceil(cursorX * NUM_OF_WORKSPACES / widgetWidth);
|
||||
Hyprland.sendMessage(`dispatch workspace ${wsId}`)
|
||||
})
|
||||
self.on('button-press-event', (self, event) => {
|
||||
self._clicked = true;
|
||||
const [_, cursorX, cursorY] = event.get_coords();
|
||||
const widgetWidth = self.get_allocation().width;
|
||||
const wsId = Math.ceil(cursorX * NUM_OF_WORKSPACES / widgetWidth);
|
||||
Hyprland.sendMessage(`dispatch workspace ${wsId}`);
|
||||
})
|
||||
self.on('button-release-event', (self) => self._clicked = false);
|
||||
}
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user