diff --git a/examples/Cargo.toml b/examples/Cargo.toml index 31ac02a52e4a..219d99a60066 100644 --- a/examples/Cargo.toml +++ b/examples/Cargo.toml @@ -130,6 +130,10 @@ path = "list_box_model/main.rs" name = "list_store" path = "list_store/main.rs" +[[bin]] +name = "header_bar" +path = "header_bar/main.rs" + [[bin]] name = "menu_bar" path = "menu_bar/main.rs" diff --git a/examples/header_bar/README.md b/examples/header_bar/README.md new file mode 100644 index 000000000000..c1d13225d35e --- /dev/null +++ b/examples/header_bar/README.md @@ -0,0 +1,11 @@ +# HeaderBar example + +This example demonstrates how to use HeaderBar in Windows. + +Run it by executing: + +```bash +cargo run --bin header_bar +``` + +![screenshot](screenshot.png) \ No newline at end of file diff --git a/examples/header_bar/main.rs b/examples/header_bar/main.rs new file mode 100644 index 000000000000..d84fb01c8451 --- /dev/null +++ b/examples/header_bar/main.rs @@ -0,0 +1,46 @@ +mod title_bar; + +use gtk::gio; +use gtk::prelude::*; +use gtk::CheckButton; +use gtk::Label; +use gtk::Stack; +use gtk::{ApplicationWindow, WindowPosition}; +use title_bar::TitleBar; + +fn build_ui(application: >k::Application) { + let window = ApplicationWindow::new(application); + + window.set_title("HeaderBar example"); + window.set_position(WindowPosition::Center); + + let stack = Stack::builder().expand(true).build(); + window.add(&stack); + + let check_button = CheckButton::builder() + .expand(true) + .label("check button") + .build(); + stack.add_titled(&check_button, "check_button", "check button"); + let label = Label::builder().label("Hello world").build(); + stack.add_titled(&label, "label", "label"); + + let title_bar = TitleBar::new(); + window.set_titlebar(Some(title_bar.header())); + title_bar.set_stack(stack); + + window.show_all(); +} + +fn main() { + gio::resources_register_include!("compiled.gresource").unwrap(); + + let application = gtk::Application::new( + Some("com.github.gtk-rs.examples.menu_bar"), + Default::default(), + ); + + application.connect_activate(build_ui); + + application.run(); +} diff --git a/examples/header_bar/screenshot.png b/examples/header_bar/screenshot.png new file mode 100644 index 000000000000..5b7a5700317b Binary files /dev/null and b/examples/header_bar/screenshot.png differ diff --git a/examples/header_bar/title_bar.rs b/examples/header_bar/title_bar.rs new file mode 100644 index 000000000000..600527e27002 --- /dev/null +++ b/examples/header_bar/title_bar.rs @@ -0,0 +1,27 @@ +use gtk::{prelude::*, Label}; +use gtk::{traits::StackSwitcherExt, HeaderBar, StackSwitcher}; + +pub struct TitleBar { + header: HeaderBar, + menu: StackSwitcher, +} + +impl TitleBar { + pub fn new() -> Self { + let menu = StackSwitcher::new(); + let header = HeaderBar::builder() + .custom_title(&menu) + .show_close_button(true) + .build(); + header.add(&Label::new(Some("Custom title"))); + Self { header, menu } + } + + pub fn set_stack(&self, stack: gtk::Stack) { + self.menu.set_stack(Some(&stack)); + } + + pub fn header(&self) -> &HeaderBar { + &self.header + } +}