Last active
August 25, 2025 21:15
-
-
Save MoAlyousef/ce50902cb7929760b89af60b3702fc1d to your computer and use it in GitHub Desktop.
custom dialog using fltk-rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use fltk::{ | |
app, button, | |
enums::{Color, Font, FrameType}, | |
frame, group, input, | |
prelude::*, | |
window, | |
}; | |
fn style_button(btn: &mut button::Button) { | |
btn.set_color(Color::Cyan); | |
btn.set_frame(FrameType::RFlatBox); | |
btn.clear_visible_focus(); | |
} | |
pub fn show_dialog() -> MyDialog { | |
MyDialog::default() | |
} | |
pub struct MyDialog { | |
inp: input::Input, | |
} | |
impl MyDialog { | |
pub fn default() -> Self { | |
let mut win = window::Window::default() | |
.with_size(400, 100) | |
.with_label("My Dialog"); | |
win.set_color(Color::from_rgb(240, 240, 240)); | |
let mut pack = group::Pack::default() | |
.with_size(300, 30) | |
.center_of_parent() | |
.with_type(group::PackType::Horizontal); | |
pack.set_spacing(20); | |
frame::Frame::default() | |
.with_size(80, 0) | |
.with_label("Enter name:"); | |
let mut inp = input::Input::default().with_size(100, 0); | |
inp.set_frame(FrameType::FlatBox); | |
let mut ok = button::Button::default().with_size(80, 0).with_label("Ok"); | |
style_button(&mut ok); | |
pack.end(); | |
win.end(); | |
win.make_modal(true); | |
win.show(); | |
ok.set_callback({ | |
let mut win = win.clone(); | |
move |_| { | |
win.hide(); | |
} | |
}); | |
while win.shown() { | |
app::wait(); | |
} | |
Self { inp } | |
} | |
pub fn value(&self) -> String { | |
self.inp.value() | |
} | |
} | |
fn main() { | |
let a = app::App::default(); | |
app::set_font(Font::Times); | |
let mut win = window::Window::default().with_size(600, 400); | |
win.set_color(Color::from_rgb(240, 240, 240)); | |
let mut btn = button::Button::default() | |
.with_size(80, 30) | |
.with_label("Click") | |
.center_of_parent(); | |
style_button(&mut btn); | |
let mut frame = frame::Frame::new(btn.x() - 40, btn.y() - 100, btn.w() + 80, 30, None); | |
frame.set_frame(FrameType::BorderBox); | |
frame.set_color(Color::Red.inactive()); | |
win.end(); | |
win.show(); | |
btn.set_callback(move |_| { | |
let d = show_dialog(); | |
frame.set_label(&d.value()); | |
}); | |
a.run().unwrap(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment