Skip to content

Commit 27e8bf1

Browse files
Split slint components
1 parent cfb2f09 commit 27e8bf1

22 files changed

Lines changed: 774 additions & 826 deletions

src/event_loop/event_loop.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::sync::Arc;
33
use moonraker_rs::{connector::websocket_read::{MoonrakerEvent, PrinterEvent}, moonraker_connection::{MoonrakerConnection, WebsocketEvent}};
44
use slint::{ComponentHandle, Weak};
55

6-
use crate::{application_error::ApplicationError, AppState, AppWindow};
6+
use crate::{application_error::ApplicationError, Webhooks, AppWindow};
77

88
pub struct EventLoop
99
{
@@ -60,7 +60,7 @@ impl EventLoop
6060
async fn on_connected(&mut self) -> Result<(), ApplicationError>
6161
{
6262
self.ui_weak
63-
.upgrade_in_event_loop(move |ui| ui.global::<AppState>().set_moonraker_connected(true))?;
63+
.upgrade_in_event_loop(move |ui| ui.global::<Webhooks>().set_moonraker_connected(true))?;
6464

6565
Ok(())
6666
}
@@ -69,9 +69,9 @@ impl EventLoop
6969
{
7070
self.ui_weak
7171
.upgrade_in_event_loop(move |ui| {
72-
ui.global::<AppState>().set_moonraker_connected(false);
73-
ui.global::<AppState>().set_klipper_state("".into());
74-
ui.global::<AppState>().set_klipper_state_message("".into());
72+
ui.global::<Webhooks>().set_moonraker_connected(false);
73+
ui.global::<Webhooks>().set_klipper_state("".into());
74+
ui.global::<Webhooks>().set_klipper_state_message("".into());
7575
})?;
7676

7777
Ok(())

src/event_loop/klipper_state.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
use moonraker_rs::connector::websocket_read::PrinterEvent;
1+
use moonraker_rs::{connector::websocket_read::PrinterEvent};
22
use slint::{ComponentHandle, SharedString};
33

4-
use crate::{application_error::ApplicationError, event_loop::EventLoop};
4+
use crate::{application_error::ApplicationError, event_loop::EventLoop, Webhooks};
55

66
impl EventLoop {
77
pub fn handle_klipper_state_updates(
@@ -13,8 +13,8 @@ impl EventLoop {
1313
let state_message = SharedString::from(&webhooks.state_message);
1414

1515
self.ui_weak.upgrade_in_event_loop(move |ui| {
16-
ui.global::<crate::AppState>().set_klipper_state(state);
17-
ui.global::<crate::AppState>().set_klipper_state_message(state_message);
16+
ui.global::<Webhooks>().set_klipper_state(state);
17+
ui.global::<Webhooks>().set_klipper_state_message(state_message);
1818
})?;
1919
}
2020

src/main.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
// Prevent console window in addition to Slint window in Windows release builds when, e.g., starting the app via file manager. Ignored on other platforms.
22
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]
33

4-
use std::{cmp::Ordering, error::Error, fs, path::PathBuf, process::exit, rc::Rc, sync::Arc};
4+
use std::{error::Error, fs, path::PathBuf, process::exit, sync::Arc};
55

66
use clap::Parser;
77
use moonraker_rs::{
8-
cache::Cache, connector::{read_deserialize::OptionalPrinterEvent, websocket_read::{MoonrakerEvent, PrinterEvent}}, moonraker_connection::WebsocketEvent, printer_objects::{NamedOptionalTemperatureFan, OptionalExtruder, OptionalHeaterBed, OptionalTemperatureFan, TemperatureConfiguration}, requests::{FileManagementRequestHandler, PrinterAdministrationRequestHandler}
8+
cache::Cache, connector::{read_deserialize::OptionalPrinterEvent}, printer_objects::{NamedOptionalTemperatureFan, OptionalExtruder, OptionalHeaterBed, OptionalTemperatureFan, TemperatureConfiguration},
99
};
10-
use slint::{Image, Model, ModelRc, Rgba8Pixel, SharedPixelBuffer, SharedString, VecModel};
11-
use tokio::sync::Mutex;
1210

1311
use crate::{config::{MoonrakerConfig, OptionalGcodeCommands}, event_loop::EventLoop, hardware::init_display, ui_functions::{register_extruder_extrude, register_extruder_load_filament, register_extruder_retract, register_extruder_unload_filament, register_filesystem_download_thumbnails, register_filesystem_list_files, register_printer_emergency_stop, register_printer_firmware_restart, register_printer_restart, register_temperature_set_new_target_temperature, register_util_format_bytes, register_util_image_exists, register_util_prettify_name}};
1412

@@ -67,7 +65,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
6765
),
6866
);
6967
let ui = init_display(&config.display)?;
70-
ui.global::<AppState>().set_moonraker_connected(false);
68+
ui.global::<Webhooks>().set_moonraker_connected(false);
7169
let ui_weak = ui.as_weak();
7270
let mut event_loop = EventLoop::new(ui_weak.clone(), moonraker_connection.clone());
7371

src/ui_functions/filesystem_download_thumbnail.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use moonraker_rs::{moonraker_connection::{MoonrakerConnection}, requests::FileMa
44
use slint::{ComponentHandle, Image, Model, Rgba8Pixel, SharedPixelBuffer, VecModel};
55
use tokio::sync::Mutex;
66

7-
use crate::{AppWindow, Filesystem, MoonrakerFileTest};
7+
use crate::{AppWindow, Filesystem, MoonrakerFile};
88

99
pub fn register_filesystem_download_thumbnails(ui: &AppWindow, moonraker_connection : &Arc<MoonrakerConnection>)
1010
{
@@ -23,14 +23,14 @@ pub fn register_filesystem_download_thumbnails(ui: &AppWindow, moonraker_connect
2323
let ui = ui_weak.upgrade().unwrap();
2424

2525
let f = ui.global::<Filesystem>().get_files();
26-
let files = f.as_any().downcast_ref::<VecModel<MoonrakerFileTest>>();
26+
let files = f.as_any().downcast_ref::<VecModel<MoonrakerFile>>();
2727

2828
let non_optional_files = match files {
2929
Some(files) => files,
3030
None => return,
3131
};
3232

33-
let mut unwrapped_files: Vec<MoonrakerFileTest> =
33+
let mut unwrapped_files: Vec<MoonrakerFile> =
3434
non_optional_files.iter().collect();
3535

3636
if global_index < unwrapped_files.len() {

src/ui_functions/filesystem_list_files.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::{cmp::Ordering, rc::Rc, sync::Arc};
33
use moonraker_rs::{moonraker_connection::MoonrakerConnection, requests::FileManagementRequestHandler};
44
use slint::{ComponentHandle, Image, ModelRc, SharedString, VecModel};
55

6-
use crate::{AppWindow, Filesystem, MoonrakerFileTest};
6+
use crate::{AppWindow, Filesystem, MoonrakerFile};
77

88
pub fn register_filesystem_list_files(ui : &AppWindow, moonraker_connection : &Arc<MoonrakerConnection>)
99
{
@@ -41,9 +41,9 @@ pub fn register_filesystem_list_files(ui : &AppWindow, moonraker_connection : &A
4141

4242
let ui = ui_weak.upgrade().unwrap();
4343

44-
let converted_files: Vec<MoonrakerFileTest> = files
44+
let converted_files: Vec<MoonrakerFile> = files
4545
.iter()
46-
.map(|f| MoonrakerFileTest {
46+
.map(|f| MoonrakerFile {
4747
path: SharedString::from(&f.path),
4848
modified: f.modified,
4949
size: f.size,

src/ui_functions/util_image_exists.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@ pub fn register_util_image_exists(ui : &AppWindow)
77
ui.global::<Utils>().on_image_exists(|f| {
88
return f.size().area() > 0;
99
});
10-
}
10+
}

ui/components/bottom-bar.slint

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { Palette } from "std-widgets.slint";
2+
import { DisplayStatus } from "../state.slint";
3+
4+
export component BottomBar inherits Rectangle {
5+
height: 20px;
6+
width: 100%;
7+
background: Palette.alternate-background;
8+
in property <string> text;
9+
10+
HorizontalLayout {
11+
Text {
12+
text: text;
13+
horizontal-alignment: center;
14+
vertical-alignment: center;
15+
color: Palette.alternate-foreground;
16+
}
17+
}
18+
}
19+
20+
export component BottomBarWithStatusMessage inherits BottomBar {
21+
text: DisplayStatus.message;
22+
height: DisplayStatus.message == "" ? 0px : 20px;
23+
}

ui/components/ghost-button.slint

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { Palette } from "std-widgets.slint";
2+
3+
export component GhostButton inherits VerticalLayout {
4+
in property <string> text;
5+
in property <color> text-color: Palette.foreground;
6+
callback clicked;
7+
8+
Rectangle {
9+
height: t.height * 2;
10+
min-width: t.width * 1.5;
11+
//background: red;
12+
13+
t := Text {
14+
text: text;
15+
color: text-color;
16+
font-weight: 600;
17+
}
18+
19+
TouchArea {
20+
clicked => { root.clicked(); }
21+
}
22+
}
23+
}

ui/components/icon-button.slint

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import { Palette, StyleMetrics } from "std-widgets.slint";
2+
3+
export component IconButton inherits Rectangle {
4+
in property <image> icon <=> img.source;
5+
in property <bool> checked: false;
6+
in property <brush> foreground: checked ? Palette.accent-foreground : Palette.foreground;
7+
in property <brush> bg: checked ? Palette.accent-background : transparent;
8+
9+
states [
10+
pressed when touch-area.pressed : {
11+
root.opacity: 0.5;
12+
}
13+
]
14+
15+
callback clicked();
16+
17+
background: bg;
18+
19+
img := Image {
20+
horizontal-alignment: center;
21+
vertical-alignment: center;
22+
colorize: foreground;
23+
}
24+
25+
touch-area := TouchArea {
26+
clicked => { root.clicked(); }
27+
}
28+
}
29+
30+
export component RightSideIconButton inherits IconButton {
31+
border-top-left-radius: 0;
32+
border-bottom-left-radius: 0;
33+
border-top-right-radius: StyleMetrics.layout-padding;
34+
border-bottom-right-radius: StyleMetrics.layout-padding;
35+
}
36+
37+
export component LeftSideIconButton inherits IconButton {
38+
border-top-left-radius: StyleMetrics.layout-padding;
39+
border-bottom-left-radius: StyleMetrics.layout-padding;
40+
border-top-right-radius: 0;
41+
border-bottom-right-radius: 0;
42+
}

ui/components/number-pad.slint

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
import { VirtualKeyboardButton } from "../virtual_keyboard.slint";
2+
import { Icons } from "../constants.slint";
3+
import { StyleMetrics } from "std-widgets.slint";
4+
import { TemperatureSensors } from "../state.slint";
5+
6+
export component NumberPad inherits Rectangle
7+
{
8+
in property <string> name;
9+
callback accept(number: int);
10+
callback cancel();
11+
property <int> current-number: 0;
12+
in property <bool> can_go_negative: false;
13+
14+
function add(key: int) {
15+
if (current-number * 10 + key > 999
16+
|| current-number * 10 - key < -999) {
17+
return;
18+
}
19+
20+
current-number = current-number >= 0
21+
? current-number * 10 + key
22+
: current-number * 10 - key;
23+
}
24+
25+
function remove() {
26+
current-number = current-number >= 0
27+
? floor(current-number / 10)
28+
: ceil(current-number / 10);
29+
}
30+
31+
VerticalLayout {
32+
spacing: 5px;
33+
34+
HorizontalLayout {
35+
spacing: 5px;
36+
vertical-stretch: 1;
37+
38+
Text {
39+
text: name + ": " + current-number;
40+
horizontal-alignment: center;
41+
vertical-alignment: center;
42+
font-size: 20px;
43+
font-weight: 900;
44+
horizontal-stretch: 5;
45+
}
46+
47+
VirtualKeyboardButton { icon: Icons.backspace; key-pressed => { remove(); } horizontal-stretch: 1; }
48+
49+
if can_go_negative: VirtualKeyboardButton { key: current-number >= 0 ? "-" : "+"; key-pressed => { current-number *= -1; } }
50+
}
51+
52+
HorizontalLayout {
53+
spacing: 5px;
54+
vertical-stretch: 1;
55+
VirtualKeyboardButton { key: "1"; key-pressed => { add(1) } }
56+
VirtualKeyboardButton { key: "2"; key-pressed => { add(2) } }
57+
VirtualKeyboardButton { key: "3"; key-pressed => { add(3) } }
58+
VirtualKeyboardButton { icon: Icons.check; key-pressed => { accept(current-number); current-number = 0; } }
59+
}
60+
61+
HorizontalLayout {
62+
spacing: 5px;
63+
vertical-stretch: 1;
64+
VirtualKeyboardButton { key: "4"; key-pressed => { add(4) } }
65+
VirtualKeyboardButton { key: "5"; key-pressed => { add(5) } }
66+
VirtualKeyboardButton { key: "6"; key-pressed => { add(6) } }
67+
VirtualKeyboardButton { icon: Icons.close; key-pressed => { cancel(); } }
68+
}
69+
70+
HorizontalLayout {
71+
spacing: 5px;
72+
vertical-stretch: 1;
73+
VirtualKeyboardButton { key: "7"; key-pressed => { add(7) } }
74+
VirtualKeyboardButton { key: "8"; key-pressed => { add(8) } }
75+
VirtualKeyboardButton { key: "9"; key-pressed => { add(9) } }
76+
VirtualKeyboardButton { key: "0"; key-pressed => { add(0) } }
77+
}
78+
}
79+
}
80+
81+
export component TemperatureEntry
82+
{
83+
out property <bool> is_keyboard_open: false;
84+
property <string> internal_temp_device_name;
85+
property <string> friendly_temp_device_name;
86+
87+
public function open_keyboard(internal_name: string, friendly_name: string) {
88+
internal_temp_device_name = internal_name;
89+
friendly_temp_device_name = friendly_name;
90+
is_keyboard_open = true;
91+
}
92+
93+
width: 100%;
94+
height: 100%;
95+
96+
if is_keyboard_open: GridLayout {
97+
padding-left: StyleMetrics.layout-padding / 2;
98+
padding-right: StyleMetrics.layout-padding / 2;
99+
NumberPad {
100+
name: friendly_temp_device_name;
101+
accept(number) => {
102+
TemperatureSensors.set_new_target_temperature(internal_temp_device_name, number);
103+
is_keyboard_open = false;
104+
}
105+
cancel() => {
106+
is_keyboard_open = false;
107+
}
108+
}
109+
}
110+
}

0 commit comments

Comments
 (0)