changelog shortlog graph tags branches changeset files revisions annotate raw help

Mercurial > core / rust/ui/alik/src/lib.rs

changeset 698: 96958d3eb5b0
parent: 957915316dea
author: Richard Westhaver <ellis@rwest.io>
date: Fri, 04 Oct 2024 22:04:59 -0400
permissions: -rw-r--r--
description: fixes
1 //! alik/src/lib.rs --- Alik UI library
2 
3 //
4 
5 //! Code:
6 
7 // We derive Deserialize/Serialize so we can persist app state on shutdown.
8 #[derive(serde::Deserialize, serde::Serialize)]
9 #[serde(default)] // if we add new fields, give them default values when deserializing old state
10 pub struct AlikApp {
11  // Example stuff:
12  label: String,
13 
14  #[serde(skip)] // This how you opt-out of serialization of a field
15  value: f32,
16 }
17 
18 impl Default for AlikApp {
19  fn default() -> Self {
20  Self {
21  // Example stuff:
22  label: "Hello World!".to_owned(),
23  value: 2.7,
24  }
25  }
26 }
27 
28 impl AlikApp {
29  /// Called once before the first frame.
30  pub fn new(cc: &eframe::CreationContext<'_>) -> Self {
31  // This is also where you can customize the look and feel of egui using
32  // `cc.egui_ctx.set_visuals` and `cc.egui_ctx.set_fonts`.
33 
34  // Load previous app state (if any).
35  // Note that you must enable the `persistence` feature for this to work.
36  if let Some(storage) = cc.storage {
37  return eframe::get_value(storage, eframe::APP_KEY).unwrap_or_default();
38  }
39 
40  Default::default()
41  }
42 }
43 
44 impl eframe::App for AlikApp {
45  /// Called by the frame work to save state before shutdown.
46  fn save(&mut self, storage: &mut dyn eframe::Storage) {
47  eframe::set_value(storage, eframe::APP_KEY, self);
48  }
49 
50  /// Called each time the UI needs repainting, which may be many times per
51  /// second.
52  fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
53  // Put your widgets into a `SidePanel`, `TopBottomPanel`, `CentralPanel`,
54  // `Window` or `Area`. For inspiration and more examples, go to https://emilk.github.io/egui
55 
56  egui::TopBottomPanel::top("top_panel").show(ctx, |ui| {
57  // The top panel is often a good place for a menu bar:
58 
59  egui::menu::bar(ui, |ui| {
60  // NOTE: no File->Quit on web pages!
61  let is_web = cfg!(target_arch = "wasm32");
62  if !is_web {
63  ui.menu_button("File", |ui| {
64  if ui.button("Quit").clicked() {
65  ctx.send_viewport_cmd(egui::ViewportCommand::Close);
66  }
67  });
68  ui.add_space(16.0);
69  }
70 
71  egui::widgets::global_dark_light_mode_buttons(ui);
72  });
73  });
74 
75  egui::CentralPanel::default().show(ctx, |ui| {
76  // The central panel the region left after adding TopPanel's and
77  // SidePanel's
78  ui.heading("alik");
79 
80  ui.horizontal(|ui| {
81  ui.label("query: ");
82  ui.text_edit_singleline(&mut self.label);
83  });
84 
85  ui.add(egui::Slider::new(&mut self.value, 0.0..=10.0).text("val"));
86  if ui.button("inc").clicked() {
87  self.value += 1.0;
88  }
89 
90  ui.separator();
91 
92  ui.add(egui::github_link_file!(
93  "https://vc.compiler.company/comp/core/-/blob/branch/default/rust/",
94  "Source code."
95  ));
96 
97  ui.with_layout(egui::Layout::bottom_up(egui::Align::LEFT), |ui| {
98  egui::warn_if_debug_build(ui);
99  });
100  });
101  }
102 }