///| /// The saved home layout as JSON: /// `{"v":1,"slots":[{"kind":"app","str":COMPONENT,"x":-1,"y":-1}, /// {"kind":"widget","str":LABEL,"wid":ID,"w":COLS,"rows":ROWS,"x":-1,"y":-1}]}`. pub fn save_json(home : Array[@grid.Slot]) -> String { let slots : Array[Json] = home.map(fn(s) { match s.kind { App => { "kind": "app", "str": s.str, "x": s.x, "y": s.y } Widget => { "kind": "widget", "str": s.str, "wid": s.wid, "w": s.w, "rows": s.rows, "x": s.x, "y": s.y, } } }) Json::object({ "v": 1, "slots": Json::array(slots) }).stringify() } ///| /// Read a saved layout; `None` if the text is not one. Slots without a name are /// dropped, and no more than `home_cap` are kept. pub fn load_json(text : String, next_key : Ref[Int]) -> Array[@grid.Slot]? { guard @jsonx.parse(text) is Some(j) else { return None } guard @jsonx.get(j, "slots") is Some(Array(_)) else { return None } let out : Array[@grid.Slot] = [] for s in @jsonx.arr(j, "slots") { let str = @jsonx.str(s, "str") if str == "" || out.length() >= home_cap { continue } let slot = if @jsonx.str(s, "kind") == "widget" { @grid.Slot::widget( str, @jsonx.int(s, "wid"), w=@cmp.maximum(0, @jsonx.int(s, "w")), rows=@cmp.maximum(1, @jsonx.int(s, "rows", default=1)), key=take_key(next_key), ) } else { @grid.Slot::app(str, key=take_key(next_key)) } slot.x = @jsonx.int(s, "x", default=-1) slot.y = @jsonx.int(s, "y", default=-1) out.push(slot) } Some(out) } ///| fn take_key(next_key : Ref[Int]) -> Int { let k = next_key.val next_key.val = k + 1 k } ///| /// The WebView build kept its home in localStorage as `ultra.home.n` and, for slot i, /// `ultra.home.{kind,str,wid,h,w,cx,cy}.i` (all strings; kind 1 is a widget, `h` its /// height in px, `cx`/`cy` the pinned cell). `text` is those keys as a JSON object. Widget /// heights in px become whole rows. pub fn legacy_import(text : String, next_key : Ref[Int]) -> Array[@grid.Slot] { guard @jsonx.parse(text) is Some(kv) else { return [] } fn num(k : String, i : Int, d : Int) -> Int { match @jsonx.get(kv, "ultra.home.\{k}.\{i}") { Some(String(s)) => try @string.parse_int(s) catch { _ => d } Some(Number(n, ..)) => n.to_int() _ => d } } let n = num2(kv, "ultra.home.n") let out : Array[@grid.Slot] = [] for i in 0.. s _ => "" } if str == "" || out.length() >= home_cap { continue } let slot = if num("kind", i, 0) == 1 { @grid.Slot::widget( str, num("wid", i, 0), w=@cmp.maximum(0, num("w", i, 0)), rows=@grid.widget_rows(num("h", i, 120)), key=take_key(next_key), ) } else { @grid.Slot::app(str, key=take_key(next_key)) } slot.x = num("cx", i, -1) slot.y = num("cy", i, -1) out.push(slot) } out } ///| fn num2(kv : Json, key : String) -> Int { match @jsonx.get(kv, key) { Some(String(s)) => try @string.parse_int(s) catch { _ => 0 } Some(Number(n, ..)) => n.to_int() _ => 0 } } ///| /// The apps.json the shell (or the demo) serves: `{"apps": [{"label", "component", /// "icon", "system"}]}`, capped at `app_cap`. pub fn parse_apps(text : String) -> Array[AppInfo]? { guard @jsonx.parse(text) is Some(j) else { return None } guard @jsonx.get(j, "apps") is Some(Array(_)) else { return None } let out : Array[AppInfo] = [] for a in @jsonx.arr(j, "apps") { if out.length() >= app_cap { break } out.push({ label: @jsonx.str(a, "label"), component: @jsonx.str(a, "component"), icon: @jsonx.str(a, "icon"), system: @jsonx.bool(a, "system"), }) } Some(out) } ///| /// widgets.json: `{"widgets": [{"label", "app", "provider", "icon", "h"}]}`. pub fn parse_widgets(text : String) -> Array[WidgetInfo]? { guard @jsonx.parse(text) is Some(j) else { return None } guard @jsonx.get(j, "widgets") is Some(Array(_)) else { return None } let out : Array[WidgetInfo] = [] for w in @jsonx.arr(j, "widgets") { if out.length() >= 256 { break } out.push({ label: @jsonx.str(w, "label"), provider: @jsonx.str(w, "provider"), icon: @jsonx.str(w, "icon"), app: @jsonx.str(w, "app"), h: @jsonx.int(w, "h"), }) } Some(out) } ///| /// The shell's `widgetLimits` JSON (`{"w","minW","minH","maxW","maxH","h","v"}`); /// missing fields take the defaults. pub fn parse_limits(text : String) -> Limits { guard @jsonx.parse(text) is Some(j) else { return default_limits } { w: @jsonx.int(j, "w"), min_w: @jsonx.int(j, "minW"), min_h: @jsonx.int(j, "minH"), max_w: @jsonx.int(j, "maxW"), max_h: @jsonx.int(j, "maxH"), h: @jsonx.bool(j, "h", default=true), v: @jsonx.bool(j, "v", default=true), } }