main.rs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. use gtk::glib;
  2. use gtk::prelude::*;
  3. const APP_ID: &str = "org.gtk_rs.HelloWorld1";
  4. fn main() -> glib::ExitCode {
  5. // Create a new application
  6. let app = gtk::Application::builder().application_id(APP_ID).build();
  7. app.connect_activate(build_ui);
  8. // Run the application
  9. app.run()
  10. }
  11. pub struct App {
  12. pub window: gtk::Window,
  13. pub container: gtk::Paned,
  14. pub center: gtk::Button,
  15. pub toolbar: gtk::Button,
  16. pub header: Header,
  17. }
  18. impl App {
  19. fn new() -> App {
  20. let window = gtk::Window::new();
  21. let container = gtk::Paned::new(gtk::Orientation::Horizontal);
  22. let header = Header::new();
  23. let toolbar = gtk::Button::builder()
  24. .label("Press me!")
  25. .margin_top(12)
  26. .margin_bottom(12)
  27. .margin_start(12)
  28. .margin_end(12)
  29. .build();
  30. let center = gtk::Button::builder()
  31. .label("Press me!")
  32. .margin_top(12)
  33. .margin_bottom(12)
  34. .margin_start(12)
  35. .margin_end(12)
  36. .build();
  37. window.set_titlebar(Some(&header.container));
  38. window.set_title(Some("Thyme"));
  39. container.set_start_child(Some(&toolbar));
  40. container.set_end_child(Some(&center));
  41. window.set_child(Some(&container));
  42. App {
  43. window,
  44. center,
  45. container,
  46. toolbar,
  47. header,
  48. }
  49. }
  50. }
  51. pub struct Header {
  52. pub container: gtk::HeaderBar,
  53. pub open_btn: gtk::Button,
  54. pub save_btn: gtk::Button,
  55. pub save_as_btn: gtk::Button,
  56. }
  57. impl Header {
  58. fn new() -> Header {
  59. let container = gtk::HeaderBar::new();
  60. let open_btn = gtk::Button::with_label("open");
  61. let save_btn = gtk::Button::with_label("save");
  62. let save_as_btn = gtk::Button::with_label("save as");
  63. open_btn.connect_clicked(Header::do_open);
  64. container.pack_start(&open_btn);
  65. container.pack_end(&save_btn);
  66. container.pack_end(&save_as_btn);
  67. Header {
  68. container,
  69. open_btn,
  70. save_btn,
  71. save_as_btn,
  72. }
  73. }
  74. fn do_open(_: &gtk::Button) {
  75. let _open_dialog = gtk::FileChooserDialog::new(
  76. Some("Open"),
  77. Some(&gtk::Window::new()),
  78. gtk::FileChooserAction::Open,
  79. &[
  80. ("Cancel", gtk::ResponseType::Cancel),
  81. ("Open", gtk::ResponseType::Ok),
  82. ],
  83. );
  84. }
  85. }
  86. fn build_ui(app: &gtk::Application) {
  87. let thyme_app = App::new();
  88. thyme_app.window.set_application(Some(app));
  89. thyme_app.window.present();
  90. }