xlmnxp / blue-recorder

Simple Screen Recorder written in Rust based on Green Recorder
Other
525 stars 26 forks source link

Project crashes due unavalibity of ui files when compiling app from crates.io #25

Closed qarmin closed 2 years ago

qarmin commented 2 years ago

The problem is that ui is loaded in runtime.

GTK-rs allows to include ui into binary:

        let glade_src = include_str!("../ui/main_window.glade").to_string();
        let builder = Builder::from_string(glade_src.as_str());

        //// Windows
        let window_main: gtk::Window = builder.object("window_main").unwrap();

https://github.com/qarmin/czkawka/blob/aeeb59d9a6b3429f4c35a9c1f71914293b4981d6/czkawka_gui/src/gui_data.rs#L85-L89

xlmnxp commented 2 years ago

that will not work as we expect it include_str! work in compile not in runtime

qarmin commented 2 years ago

Yes it works in compile time(builder don't need to be set in runtime), so this code https://github.com/xlmnxp/blue-recorder/blob/58585c3210be6707597d0cbcdcb5214638b2c95f/src/main.rs#L36-L52

 let builder: Builder; 
 let user_interface_path_abs = { 
     let mut current_exec_dir = std::env::current_exe().unwrap(); 
     current_exec_dir.pop(); 
     current_exec_dir 
 } 
 .join(Path::new("interfaces/main.ui")); 

 if user_interface_path_abs.exists() { 
     builder = Builder::from_file(user_interface_path_abs); 
 } else { 
     builder = Builder::from_file( 
         std::env::var("INTERFACES_DIR") 
             .unwrap_or(String::from("interfaces/")) 
             .add("main.ui"), 
     ); 
 } 

could be simplified to

        let ui_src = include_str!("../interfaces/main.ui").to_string();
        let builder = Builder::from_string(ui_src.as_str());
xlmnxp commented 2 years ago

Yes it works in compile time(builder don't need to be set in runtime), so this code

https://github.com/xlmnxp/blue-recorder/blob/58585c3210be6707597d0cbcdcb5214638b2c95f/src/main.rs#L36-L52

 let builder: Builder; 
 let user_interface_path_abs = { 
     let mut current_exec_dir = std::env::current_exe().unwrap(); 
     current_exec_dir.pop(); 
     current_exec_dir 
 } 
 .join(Path::new("interfaces/main.ui")); 

 if user_interface_path_abs.exists() { 
     builder = Builder::from_file(user_interface_path_abs); 
 } else { 
     builder = Builder::from_file( 
         std::env::var("INTERFACES_DIR") 
             .unwrap_or(String::from("interfaces/")) 
             .add("main.ui"), 
     ); 
 } 

could be simplified to

        let ui_src = include_str!("../interfaces/main.ui").to_string();
        let builder = Builder::from_string(ui_src.as_str());

That will remove the need to INTERFACES_DIR environment

xlmnxp commented 2 years ago

I did it 👍🏼

xlmnxp commented 2 years ago

thank you