Files
chandra/components/network/gen/interface/chandra/desktop/plasmoid/stub.mbt
T
nandi 52b42890d6
CI / test (push) Canceled after 0s
Camera: C ISP pipeline, contrast autofocus, and VFR video
- Add chandra-camera-isp.c (C/OpenMP) for Quad-Bayer unpack, CCM, and tone mapping, with a NumPy fallback when it is not built
- Add non-blocking contrast-detection VCM autofocus, with lens subdev discovery on the sensor's I2C bus
- Tune Quad-Bayer AE: a video-mode vblank cap, damped steps, and full analog gain before extending blanking
- Record variable-frame-rate video through a streaming Matroska muxer, and count dropped frames
- Ship libcamera IPA tuning files for imx376, imx519, and uncalibrated sensors
- Map camera buffers by fd and sync them with dma-buf ioctls
2026-11-28 19:25:43 -08:00

522 lines
15 KiB
MoonBit

///| Implementation of `chandra:desktop/plasmoid` for the network module. The panel button shows the
/// connection: Wi-Fi signal bars and the network's name, a plug for a wired connection, crossed-out
/// bars offline or in airplane mode. A click opens a popup with the status (and the outcome of the
/// last action), switches for Wi-Fi and airplane mode, and the Wi-Fi networks in range: click one to
/// connect (a saved or open network at once; a new secured one asks for its password in a field
/// that opens under it), click the one in use to disconnect, or "Forget" a saved one. Opening the
/// popup asks for a scan. All of it goes through the `network` capability; without NetworkManager
/// the popup only shows the status. Up/Down move the highlight, Enter picks, Escape closes.
///| One popup row.
priv enum Row {
Status
Message(String)
WifiSwitch(Bool)
AirplaneSwitch(Bool)
Heading(String)
Network(@net.WifiNetwork)
Password
}
///| Popup geometry.
let popup_w : Int = 320
///|
let row_h : Int = 30
///|
let margin : Int = 6
///| The most networks listed.
let max_networks : Int = 10
///| Width of the "Forget" / "Disconnect" zone at the right of a network row.
let action_w : Int = 84
///|
let open_ : Ref[Bool] = { val: false }
///| The highlighted row, by `row_key` ("" for none: rows come and go as networks do), and where
/// the pointer is in it.
let hover_key : Ref[String] = { val: "" }
///|
let hover_x : Ref[Int] = { val: 0 }
///| The secured network whose password is being typed ("" for none), and the password so far.
let entering : Ref[String] = { val: "" }
///|
let password : Ref[String] = { val: "" }
///| The popup's rows for the current state.
fn rows() -> Array[Row] {
let s = @net.status()
let out : Array[Row] = [Status]
if s.message != "" {
out.push(Message(s.message))
}
if !s.can_manage {
return out
}
if s.has_wifi {
out.push(WifiSwitch(s.wifi_enabled))
}
out.push(AirplaneSwitch(s.airplane))
if s.has_wifi && s.wifi_enabled {
let nets = @net.wifi_networks()
out.push(Heading(if nets.length() == 0 { "Looking for networks..." } else { "Wi-Fi networks" }))
for i = 0; i < nets.length() && i < max_networks; i = i + 1 {
out.push(Network(nets[i]))
if nets[i].ssid == entering.val {
out.push(Password)
}
}
}
out
}
///| What identifies a row across frames.
fn row_key(r : Row) -> String {
match r {
Status => "status"
Message(_) => "message"
WifiSwitch(_) => "wifi"
AirplaneSwitch(_) => "airplane"
Heading(_) => "heading"
Network(n) => "net:" + n.ssid
Password => "password"
}
}
///| Index of the highlighted row in `rs`, or -1.
fn hover_index(rs : Array[Row]) -> Int {
for i = 0; i < rs.length(); i = i + 1 {
if hover_key.val != "" && row_key(rs[i]) == hover_key.val {
return i
}
}
-1
}
///|
fn set_hover(rs : Array[Row], i : Int) -> Unit {
hover_key.val = if i >= 0 && i < rs.length() { row_key(rs[i]) } else { "" }
}
///|
fn popup_h(n : Int) -> Int {
margin * 2 + n * row_h
}
///| Whether row `r` can be picked.
fn pickable(r : Row) -> Bool {
match r {
WifiSwitch(_) | AirplaneSwitch(_) | Network(_) | Password => true
_ => false
}
}
///| Row under a popup-local point, or -1.
fn row_at(rs : Array[Row], x : Int, y : Int) -> Int {
if x < margin || x >= popup_w - margin || y < margin {
return -1
}
let i = (y - margin) / row_h
if i < rs.length() && pickable(rs[i]) { i } else { -1 }
}
///|
fn clear_entry() -> Unit {
entering.val = ""
password.val = ""
}
///|
fn close() -> Unit {
open_.val = false
hover_key.val = ""
clear_entry()
}
///| What a 0..100 signal is in bars (1-4).
fn bars_of(signal : Int) -> Int {
if signal >= 75 {
4
} else if signal >= 50 {
3
} else if signal >= 25 {
2
} else {
1
}
}
///| Four signal bars, bottom-aligned at `by`, `lit` of them in `on`, the rest in `off`.
fn bars(x : Int, by : Int, lit : Int, on : @types.Color, off : @types.Color) -> Array[@types.DrawOp] {
let ops : Array[@types.DrawOp] = []
for i = 0; i < 4; i = i + 1 {
let h = 4 + i * 3
ops.push(@ui.fill_rect(@ui.rect(x + i * 5, by - h, 3, h), if i < lit { on } else { off }, radius=1))
}
ops
}
///| An Ethernet plug centred on (cx, cy).
fn plug(cx : Int, cy : Int, c : @types.Color) -> Array[@types.DrawOp] {
[
@ui.stroke_rect(@ui.rect(cx - 6, cy - 6, 12, 9), c, width=1, radius=1),
@ui.fill_rect(@ui.rect(cx - 3, cy - 3, 2, 3), c),
@ui.fill_rect(@ui.rect(cx + 1, cy - 3, 2, 3), c),
@ui.line(cx, cy + 3, cx, cy + 7, c, width=2),
]
}
///| The connection's icon centred on (cx, cy).
fn status_icon(s : @net.NetStatus, cx : Int, cy : Int, fg : @types.Color) -> Array[@types.DrawOp] {
let dim = @ui.with_alpha(fg, 70)
match s.state {
1 => plug(cx, cy, fg)
2 => bars(cx - 9, cy + 7, bars_of(s.signal.reinterpret_as_int()), fg, dim)
3 => bars(cx - 9, cy + 7, 0, fg, @ui.with_alpha(fg, 140))
_ => {
let ops = bars(cx - 9, cy + 7, 0, fg, dim)
ops.push(@ui.line(cx - 9, cy + 7, cx + 9, cy - 7, fg, width=2))
ops
}
}
}
///| What the button says.
fn label(s : @net.NetStatus) -> String {
match s.state {
1 => "Wired"
2 => s.name
3 => "Connecting"
_ => if s.airplane { "Airplane mode" } else { "Offline" }
}
}
///| The popup's status line.
fn status_line(s : @net.NetStatus) -> String {
match s.state {
1 => if s.name == "" { "Wired" } else { "Wired: " + s.name }
2 => "Connected to " + s.name + " (" + s.signal.to_string() + "%)"
3 => if s.name == "" { "Connecting..." } else { "Connecting to " + s.name + "..." }
_ => if s.airplane { "Airplane mode" } else { "Not connected" }
}
}
///| Display-cell width of one Unicode scalar value in `fit` (0 for emoji modifiers/tags,
/// 2 for supplementary emoji, 1 otherwise).
fn fit_cells(cp : Int) -> Int {
if cp == 0xFE0E ||
cp == 0xFE0F ||
cp == 0x200D ||
cp == 0x20E3 ||
(cp >= 0x1F3FB && cp <= 0x1F3FF) ||
(cp >= 0xE0020 && cp <= 0xE007F) {
0
} else if cp >= 0x1F000 {
2
} else {
1
}
}
///| `s` cut to fit `w` pixels at font size `fs`.
fn fit(s : String, w : Int, fs : Int) -> String {
let chars = s.to_array()
let mut total = 0
for ch in chars {
total = total + fit_cells(ch.to_int())
}
let cw = (fs * 6 + 9) / 10
let n = if cw > 0 { w / cw } else { total }
if total <= n {
return s
}
if n <= 1 {
return ""
}
let kept : Array[Char] = []
let mut used = 0
for ch in chars {
let c = fit_cells(ch.to_int())
if used + c > n - 1 {
break
}
kept.push(ch)
used = used + c
}
while kept.length() > 0 && kept[kept.length() - 1].to_int() == 0x200D {
ignore(kept.pop())
}
let sb = StringBuilder::new()
for ch in kept {
sb.write_char(ch)
}
sb.write_char('…')
sb.to_string()
}
///|
pub fn info() -> PlasmoidInfo {
{
id: "org.chandra.network",
name: "Network",
preferred: { w: 130, h: 34 },
min_width: 34,
stretch: false,
}
}
///|
pub fn render(size : @types.Size) -> Array[@types.DrawOp] {
let t = @theme.get_tokens()
let w = size.w.reinterpret_as_int()
let h = size.h.reinterpret_as_int()
let r = t.radius.reinterpret_as_int()
let fs = t.font_size.reinterpret_as_int()
let bg = if open_.val { t.palette.accent } else { t.palette.surface_hover }
let fg = if open_.val { t.palette.accent_foreground } else { t.palette.foreground }
let s = @net.status()
let ops = [@ui.fill_rect(@ui.rect(0, 0, w, h), bg, radius=r)]
ops.append(status_icon(s, 17, h / 2, fg))
if w >= 70 {
ops.push(@ui.text(32, h / 2, fit(label(s), w - 38, fs - 2), fs - 2, fg))
}
ops
}
///| A switch at the right of a row whose middle is `mid`.
fn switch(w : Int, mid : Int, on : Bool, p : @theme.Palette) -> Array[@types.DrawOp] {
let sw = 34
let sh = 18
let sx = w - margin - 10 - sw
let sy = mid - sh / 2
let knob = sh / 2 - 3
if on {
[
@ui.fill_rect(@ui.rect(sx, sy, sw, sh), p.accent, radius=sh / 2),
@ui.circle(sx + sw - sh / 2, mid, knob, p.accent_foreground),
]
} else {
[
@ui.stroke_rect(@ui.rect(sx, sy, sw, sh), p.foreground_muted, width=1, radius=sh / 2),
@ui.circle(sx + sh / 2, mid, knob, p.foreground_muted),
]
}
}
///|
pub fn popup() -> PopupSurface? {
if !open_.val {
return None
}
let t = @theme.get_tokens()
let rs = rows()
let w = popup_w
let h = popup_h(rs.length())
let r = t.radius.reinterpret_as_int()
let fs = t.font_size.reinterpret_as_int()
let p = t.palette
let s = @net.status()
let hi = hover_index(rs)
let ops : Array[@types.DrawOp] = [
@ui.fill_rect(@ui.rect(0, 0, w, h), p.surface_popup, radius=r),
@ui.stroke_rect(@ui.rect(0, 0, w, h), p.border, width=1, radius=r),
]
for i = 0; i < rs.length(); i = i + 1 {
let y = margin + i * row_h
let mid = y + (row_h - 2) / 2
let hovered = i == hi
if hovered && pickable(rs[i]) && !(rs[i] is Password) {
ops.push(@ui.fill_rect(@ui.rect(margin, y, w - 2 * margin, row_h - 2), p.surface_hover, radius=r))
}
match rs[i] {
Status => {
ops.append(status_icon(s, margin + 17, mid, p.foreground))
ops.push(@ui.text(margin + 34, mid, fit(status_line(s), w - 2 * margin - 40, fs - 1), fs - 1, p.foreground, bold=true))
}
Message(m) => ops.push(@ui.text(margin + 34, mid, fit(m, w - 2 * margin - 40, fs - 3), fs - 3, p.foreground_muted))
WifiSwitch(on) => {
ops.push(@ui.text(margin + 18, mid, "Wi-Fi", fs - 1, p.foreground))
ops.append(switch(w, mid, on, p))
}
AirplaneSwitch(on) => {
ops.push(@ui.text(margin + 18, mid, "Airplane mode", fs - 1, p.foreground))
ops.append(switch(w, mid, on, p))
}
Heading(text) => ops.push(@ui.text(margin + 8, mid + 2, text, fs - 3, p.foreground_muted, bold=true))
Network(n) => {
let lit = bars_of(n.signal.reinterpret_as_int())
ops.append(bars(margin + 10, mid + 6, lit, p.foreground, @ui.with_alpha(p.foreground, 70)))
let right = if n.active && hovered {
"Disconnect"
} else if n.active {
"Connected"
} else if n.saved && hovered && hover_x.val >= w - margin - action_w {
"Forget"
} else if n.saved {
"Saved"
} else if n.secured {
"Secured"
} else {
"Open"
}
ops.push(@ui.text(margin + 36, mid, fit(n.ssid, w - 2 * margin - 36 - action_w, fs - 1), fs - 1, p.foreground, bold=n.active))
ops.push(@ui.text(w - margin - 10, mid, right, fs - 3, if right == "Forget" || right == "Disconnect" { p.accent } else { p.foreground_muted }, align=RIGHT))
}
Password => {
let bx = margin + 36
let bw = w - 2 * margin - 36 - action_w
ops.push(@ui.fill_rect(@ui.rect(bx, y + 2, bw, row_h - 6), p.surface, radius=r))
ops.push(@ui.stroke_rect(@ui.rect(bx, y + 2, bw, row_h - 6), p.accent, width=1, radius=r))
let shown = if password.val == "" {
"Password"
} else {
let sb = StringBuilder::new()
for _ in password.val {
sb.write_char('•')
}
sb.to_string()
}
let fg = if password.val == "" { p.foreground_muted } else { p.foreground }
ops.push(@ui.text(bx + 8, mid, fit(shown, bw - 16, fs - 2) + "|", fs - 2, fg))
let cx = w - margin - action_w + 6
ops.push(@ui.fill_rect(@ui.rect(cx, y + 2, action_w - 16, row_h - 6), p.accent, radius=r))
ops.push(@ui.text(cx + (action_w - 16) / 2, mid, "Connect", fs - 3, p.accent_foreground, align=CENTER))
}
}
}
Some({ size: { w: w.reinterpret_as_uint(), h: h.reinterpret_as_uint() }, ops })
}
///| Connect to the network whose password is being typed.
fn submit_password() -> Unit {
if entering.val != "" && password.val != "" {
@net.connect(entering.val, password.val)
clear_entry()
}
}
///| Carry out row `i` (`x`: where in it, popup-local).
fn pick(rs : Array[Row], i : Int, x : Int) -> Unit {
if i < 0 || i >= rs.length() {
return
}
match rs[i] {
WifiSwitch(on) => @net.set_wifi_enabled(!on)
AirplaneSwitch(on) => @net.set_airplane_mode(!on)
Network(n) =>
if n.active {
@net.disconnect()
} else if n.saved && x >= popup_w - margin - action_w {
@net.forget(n.ssid)
} else if n.saved || !n.secured {
clear_entry()
@net.connect(n.ssid, "")
} else if entering.val == n.ssid {
clear_entry()
} else {
entering.val = n.ssid
password.val = ""
}
Password => if x >= popup_w - margin - action_w { submit_password() }
_ => ()
}
}
///| The next pickable row from `i` in direction `d`, or `i` when there is none.
fn step(rs : Array[Row], i : Int, d : Int) -> Int {
let mut j = i + d
while j >= 0 && j < rs.length() {
if pickable(rs[j]) {
return j
}
j = j + d
}
i
}
///|
pub fn on_event(target : Surface, event : @types.InputEvent) -> Bool {
match (target, event) {
(WIDGET, PointerDown(_)) => {
if open_.val {
close()
} else {
open_.val = true
hover_key.val = ""
@net.scan()
}
true
}
(POPUP, PointerMove(p)) => {
let rs = rows()
let i = row_at(rs, p.x, p.y)
let zone = fn(x : Int) { x >= popup_w - margin - action_w }
let changed = i != hover_index(rs) || (i >= 0 && zone(p.x) != zone(hover_x.val))
set_hover(rs, i)
hover_x.val = p.x
changed
}
(POPUP, PointerDown(p)) => {
let rs = rows()
let i = row_at(rs, p.x, p.y)
if i >= 0 {
set_hover(rs, i)
hover_x.val = p.x
pick(rs, i, p.x)
}
true
}
(_, FocusLost) =>
if open_.val {
close()
true
} else {
false
}
(_, KeyDown(k)) =>
if !open_.val {
false
} else if entering.val != "" {
// Typing a password.
if k == "Escape" {
clear_entry()
} else if k == "Enter" {
submit_password()
} else if k == "Backspace" {
if password.val.length() > 0 {
password.val = password.val[:password.val.length() - 1].to_string()
}
} else if k == "Space" {
password.val = password.val + " "
} else if k.length() == 1 && password.val.length() < 63 {
password.val = password.val + k
}
true
} else if k == "Escape" {
close()
true
} else if k == "Enter" || k == " " || k == "Space" {
let rs = rows()
pick(rs, hover_index(rs), margin + 10)
true
} else if k == "ArrowDown" || k == "Down" {
let rs = rows()
set_hover(rs, step(rs, hover_index(rs), 1))
true
} else if k == "ArrowUp" || k == "Up" {
let rs = rows()
set_hover(rs, step(rs, hover_index(rs), -1))
true
} else {
false
}
_ => false
}
}