ui_app

source of files

1// Files (PLAN.md, F6b): file dialogs, whose handlers are kept by2// the window's request, and drops, which go to the drop area under the3// pointer, shown while something is dragged over it. An extension,4// installed by the first dialog asked for or drop area laid out, which5// installs the window's files (`install_files`: the portal, the shell's6// dialogs, the drop targets).78import std::string;9import std::vector;10import std::option;11import std::map;12import std::io;13import std::process;14import pkg::alloy::window;15import pkg::alloy::draw;1617import pkg::alloy::ui;18import pkg::alloy::ui::store;19import pkg::alloy::ui::tree;20import pkg::alloy::ui::layout;21import pkg::alloy::ui::painting;22import pkg::alloy::ui::events;23import pkg::alloy::ui::widgets;24import pkg::alloy::ui::fields;25import pkg::alloy::ui::scrolling;26import pkg::alloy::ui::running;27import pkg::alloy::ui::timers;28import pkg::alloy::ui::layers;29import pkg::alloy::ui::motion;30import pkg::alloy::ui::transitions;31import pkg::alloy::ui::languages;32import pkg::alloy::ui::extensions;33import pkg::alloy::ui::views;34import ui_app;3536// Bytes saved on the web, given the page to download.37pub type PageBytes = struct {38    pub file: ChosenFile,39    pub bytes: Vector<u8>,40};4142pub type DialogKind = enum {43    Open,44    Save,45};4647pub type AskedDialog = struct {48    pub kind: DialogKind,49    pub dialog: FileDialog,50    pub then: |=| (&var Ui, Vector<ChosenFile>),51};5253// The dialogs asked for, which the app gives the window after the event,54// the handlers of those shown by their requests, the saved files for the55// page, and the drop area something dragged over the window would drop56// on.57pub type Files : Extension = struct {58    pub dialogs: Vector<AskedDialog> = Vector<AskedDialog>::empty(),59    pub choosing: Map<u64, |=| (&var Ui, Vector<ChosenFile>)> = Map<u64,60    |=| (&var Ui, Vector<ChosenFile>)>::empty(),61    pub page_saves: Vector<PageBytes> = Vector<PageBytes>::empty(),62    pub target: Option<u32> = ::None,63};6465pub fn event(self f: &var Files, ui: &var Ui, event: &Event) {66    match (event) {67        ::FilesChosen |&chosen| {68            if (ui.files().choosing.remove(&chosen.request) is ::Some |move then|) {69                then(&var ui, copy chosen.files);70            }71        }72        ::DragEntered |&d| {73            ui.drag_event(::Some(logical(d.position, ui.scale)));74        }75        ::DragMoved |p| {76            ui.drag_event(::Some(logical(p, ui.scale)));77        }78        ::DragLeft {79            ui.drag_event(::None);80        }81        ::Dropped |&d| {82            var drop = copy d;83            drop.position = logical(d.position, ui.scale);84            ui.drop_event(&drop);85        }86        else {}87    }88}8990fn logical(p: Point, scale: f32) -> Point => Point { .x = p.x / scale, .y = p.y / scale };9192// The window's files installed, the dialogs asked for, given the window,93// and the page's saves.94pub fn show_in(self f: &var Files, ui: &var Ui, window: &var Window) {95    window.install_files();96    var dialogs = move ui.files().dialogs;97    ui.files().dialogs = Vector<AskedDialog>::empty();98    for (dialogs.items()) |&var asked| {99        const request = match (asked.kind) {100            ::Open window.open_files(move asked.dialog);101            ::Save window.save_file(move asked.dialog);102        };103        ui.files().choosing.insert(request, move asked.then);104    }105    for (ui.files().page_saves.view()) |&saved| {106        window.save_bytes(&saved.file, &saved.bytes.view());107    }108    ui.files().page_saves.clear();109}110111pub fn files(self ui: &var Ui) -> &var Files {112    if (ui.extension<Files>() is ::None) {113        ui.install(new Files {});114    }115    if (ui.extension_var<Files>() is ::Some |&var f|) {116        return &var f;117    }118    panic "pkg::alloy::ui: no files";119}120121// The drop area something dragged over the window would drop on.122pub fn drop_target(self ui: &Ui) -> Option<u32> {123    if (ui.extension<Files>() is ::Some |&f|) {124        return f.target;125    }126    return ::None;127}128129pub fn ask_dialog(self ui: &var Ui, asked: AskedDialog) {130    ui.files().dialogs.push(move asked);131}132133pub fn save_for_page(self ui: &var Ui, file: &ChosenFile, bytes: &[u8]) -> bool {134    if (file.path is ::Some |&path|) {135        return write_file(&path.view(), &bytes);136    }137    if (target() != "web-wasm32") {138        return false;139    }140    ui.files().page_saves.push(PageBytes {141        .file = copy file,142        .bytes = Vector<u8>::from(&bytes)143    });144    return true;145}146147/* --- Drops --- */148149// Something dragged over the window moved to the point, in logical150// pixels, or left (None): the drop area it would drop on shows it.151pub fn drag_event(self ui: &var Ui, at: Option<Point>) {152    var target = Option<u32>::None;153    if (at is ::Some |p|) {154        target = ui.drop_area_at(p);155    }156    ui.set_drop_target(target);157}158159// Something was dropped at its position, in logical pixels: it goes to160// the drop area under it, at its point in the area's coordinates.161pub fn drop_event(self ui: &var Ui, drop: &Drop) {162    const target = ui.drop_area_at(drop.position);163    ui.set_drop_target(::None);164    const index = if (target is ::Some |i|) i else {165        return;166    };167    var handler = Option<|=| (&var Ui, &Drop)>::None;168    if (ui.node(index).widget is ::Some |&w| && w is DropArea |&area|) {169        handler = ::Some(copy area.on_drop);170    }171    if (handler is ::Some |&f|) {172        const origin = ui.origin_of(index);173        var given = copy drop;174        given.position = Point { .x = drop.position.x - origin.x, .y = drop.position.y - origin.y };175        const saved = ui.handling;176        ui.handling = ::Some(index);177        f(&var ui, &given);178        ui.handling = saved;179    }180}181182fn set_drop_target(self ui: &var Ui, target: Option<u32>) {183    const old = ui.files().target;184    if (old == target) {185        return;186    }187    if (old is ::Some |index| && ui.node(index).alive) {188        ui.mark_drop_area(index, false);189    }190    ui.files().target = target;191    if (target is ::Some |index|) {192        ui.mark_drop_area(index, true);193    }194}195196fn mark_drop_area(self ui: &var Ui, index: u32, targeted: bool) {197    if (ui.node_var(index).widget is ::Some |&var w| && w is DropArea |&var area|) {198        area.targeted = targeted;199    }200    ui.mark_paint(index);201}202203// The drop area under the point, or the nearest holding what is.204fn drop_area_at(self ui: &Ui, at: Point) -> Option<u32> {205    var found = ui.hit_at(at, Hit::Any);206    while (found is ::Some |k|) {207        const node = &ui.node(k);208        if (!node.transparent && node.widget is ::Some |&widget| && widget is DropArea) {209            return found;210        }211        found = node.parent;212    }213    return ::None;214}215216/* --- The drop area --- */217218// Laid out as a stack, the Ui then following what is dragged over the219// window.220pub fn layout(self d: &var DropArea, arrange: &var Arrange, limits: Limits) -> Extent {221    arrange.ui.files();222    var stack = Stack {223        .children = &d.children,224        .padding = d.padding,225        .align = d.align,226        .sizing = d.sizing227    };228    return stack.layout(&var arrange, limits);229}230231pub fn paint(self d: &DropArea, look: &Look, paint: &var Paint) {232    paint_box(&look, &var paint, d.background, d.border, d.radius);233    if (d.targeted) {234        const area = Rect::sized(look.size.width, look.size.height);235        var tint = look.theme.focus;236        tint.a = 0.12;237        paint.rounded_rect(area, Radii::all(d.radius), Fill::Color(tint));238        paint.border(area, Radii::all(d.radius), 2.0, look.theme.focus);239    }240}241242pub fn children(self d: &DropArea) -> &[Element] => &d.children;243244pub fn sizing(self d: &DropArea) -> Sizing => d.sizing;245246pub fn renew(self d: &var DropArea, next: DropArea) -> Change {247    const targeted = d.targeted;248    d = move next;249    d.targeted = targeted;250    return Change::Layout;251}