Files
chandra/compositor/shell/desktop_widget.mbt
T
nandiandClaude Sonnet 5.5 bca69d8c90 Desktop card: click and drag it around the wallpaper
Press on the card (when no window covers it) to grab it; it follows the pointer,
clamped to the screen, with a hand cursor on hover and grabbing while dragged.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-24 23:41:31 -08:00

80 lines
2.8 KiB
MoonBit

///| Desktop widget: a card on the wallpaper (below all windows, top-right) showing
/// the time, the date and the current moon phase. Built from the same pure lib/*
/// renderers as the panel widgets.
///|
let desktop_widget_w : Int = 260
///|
let desktop_widget_h : Int = 120
///|
fn render_desktop_widget(
x0 : Int,
y0 : Int,
tokens : @theme.Tokens,
at : @types.Datetime,
out : Array[@scene.DrawOp],
) -> Unit {
let r = tokens.radius.reinterpret_as_int()
let hour = at.hour.to_int()
let minute = at.minute.to_int()
let time = @timefmt.hhmm12(hour, minute)
let date = @timefmt.weekday_short(at.weekday.to_int()) +
", " +
at.day.to_int().to_string() +
" " +
@timefmt.month_short(at.month.to_int())
let p = @moon.phase(at.year.reinterpret_as_int(), at.month.to_int(), at.day.to_int())
let ops : Array[@types.DrawOp] = [
@ui.fill_rect(@ui.rect(0, 0, desktop_widget_w, desktop_widget_h), tokens.palette.surface, radius=r),
@ui.stroke_rect(@ui.rect(0, 0, desktop_widget_w, desktop_widget_h), tokens.palette.border, width=1, radius=r),
@ui.text(18, 38, time, 32, tokens.palette.foreground, bold=true),
@ui.text(18, 74, date, tokens.font_size.reinterpret_as_int(), tokens.palette.foreground),
@ui.text(
18,
98,
@moon.phase_name(p) + " " + @moon.illumination(p).to_string() + "%",
tokens.font_size.reinterpret_as_int() - 2,
tokens.palette.foreground,
),
]
translate(ops, x0, y0, out)
translate(@moon.render_disc(@ui.size(56, 56), tokens, p), x0 + desktop_widget_w - 56 - 18, y0 + 18, out)
}
///| Default top-right position of the card, and the clamp that keeps it on screen.
fn DesktopShell::desk_pos(self : DesktopShell) -> (Int, Int) {
let (x, y) = if self.desk_set {
(self.desk_x, self.desk_y)
} else {
(self.screen_w - desktop_widget_w - 24, 24)
}
let mx = if self.screen_w > desktop_widget_w { self.screen_w - desktop_widget_w } else { 0 }
let my = if self.screen_h > desktop_widget_h { self.screen_h - desktop_widget_h } else { 0 }
(if x < 0 { 0 } else if x > mx { mx } else { x }, if y < 0 { 0 } else if y > my { my } else { y })
}
///| True if (x, y) is over the desktop card.
pub fn DesktopShell::desk_hit(self : DesktopShell, x : Int, y : Int) -> Bool {
let (cx, cy) = self.desk_pos()
x >= cx && x < cx + desktop_widget_w && y >= cy && y < cy + desktop_widget_h
}
///| Start dragging the card if the press is on it. Call only when no window covers (x, y).
pub fn DesktopShell::desk_press(self : DesktopShell, x : Int, y : Int) -> Bool {
if !self.desk_hit(x, y) {
return false
}
let (cx, cy) = self.desk_pos()
self.desk_drag = true
self.desk_off_x = x - cx
self.desk_off_y = y - cy
true
}
///| True while the card is being dragged.
pub fn DesktopShell::desk_dragging(self : DesktopShell) -> Bool {
self.desk_drag
}