1+ use std:: sync:: Arc ;
2+
3+ use moonraker_rs:: { moonraker_connection:: { MoonrakerConnection } , requests:: PrinterAdministrationRequestHandler } ;
4+ use slint:: ComponentHandle ;
5+
6+ use crate :: { config:: OptionalGcodeCommands , config:: GcodeCommands as GcodeCommandsConfig , AppWindow , GcodeCommands } ;
7+
8+
9+ pub fn run_command ( moonraker_connection : & Arc < MoonrakerConnection > , command : & str )
10+ {
11+ let command = command. to_string ( ) ;
12+ let moonraker_connection = Arc :: clone ( moonraker_connection) ;
13+ // TODO: For other callbacks that don't use the slint main thread, maybe don't run them on the slint event loop?
14+ tokio:: spawn ( async move {
15+ if let Err ( e) = moonraker_connection. run_gcode_script ( & command) . await
16+ {
17+ moonraker_connection. send_request_error ( format ! ( "Failed to send G-code command '{}': {}" , command, e) ) ;
18+ }
19+ } ) ;
20+ }
21+
22+ pub fn register_extruder_extrude ( ui : & AppWindow , moonraker_connection : & Arc < MoonrakerConnection > , gcode_command_config : & OptionalGcodeCommands )
23+ {
24+ let command = gcode_command_config. extruder_extrude . clone ( ) . unwrap_or ( GcodeCommandsConfig :: default ( ) . extruder_extrude ) ;
25+ let moonraker_connection = Arc :: clone ( moonraker_connection) ;
26+
27+ ui. global :: < GcodeCommands > ( ) . set_extruder_extrude_available ( !command. is_empty ( ) ) ;
28+ ui. global :: < GcodeCommands > ( ) . on_extruder_extrude ( move || {
29+ run_command ( & moonraker_connection, & command) ;
30+ } ) ;
31+ }
32+
33+ pub fn register_extruder_retract ( ui : & AppWindow , moonraker_connection : & Arc < MoonrakerConnection > , gcode_command_config : & OptionalGcodeCommands )
34+ {
35+ let command = gcode_command_config. extruder_retract . clone ( ) . unwrap_or ( GcodeCommandsConfig :: default ( ) . extruder_retract ) ;
36+ let moonraker_connection = Arc :: clone ( moonraker_connection) ;
37+
38+ ui. global :: < GcodeCommands > ( ) . set_extruder_retract_available ( !command. is_empty ( ) ) ;
39+ ui. global :: < GcodeCommands > ( ) . on_extruder_retract ( move || {
40+ run_command ( & moonraker_connection, & command) ;
41+ } ) ;
42+ }
43+
44+ pub fn register_extruder_load_filament ( ui : & AppWindow , moonraker_connection : & Arc < MoonrakerConnection > , gcode_command_config : & OptionalGcodeCommands )
45+ {
46+ let command = gcode_command_config. extruder_load_filament . clone ( ) . unwrap_or ( GcodeCommandsConfig :: default ( ) . extruder_load_filament ) ;
47+ let moonraker_connection = Arc :: clone ( moonraker_connection) ;
48+
49+ ui. global :: < GcodeCommands > ( ) . set_extruder_load_filament_available ( !command. is_empty ( ) ) ;
50+ ui. global :: < GcodeCommands > ( ) . on_extruder_load_filament ( move || {
51+ run_command ( & moonraker_connection, & command) ;
52+ } ) ;
53+ }
54+
55+ pub fn register_extruder_unload_filament ( ui : & AppWindow , moonraker_connection : & Arc < MoonrakerConnection > , gcode_command_config : & OptionalGcodeCommands )
56+ {
57+ let command = gcode_command_config. extruder_unload_filament . clone ( ) . unwrap_or ( GcodeCommandsConfig :: default ( ) . extruder_unload_filament ) ;
58+ let moonraker_connection = Arc :: clone ( moonraker_connection) ;
59+
60+ ui. global :: < GcodeCommands > ( ) . set_extruder_unload_filament_available ( !command. is_empty ( ) ) ;
61+ ui. global :: < GcodeCommands > ( ) . on_extruder_unload_filament ( move || {
62+ run_command ( & moonraker_connection, & command) ;
63+ } ) ;
64+ }
0 commit comments