Skip to content

Instantly share code, notes, and snippets.

@MoAlyousef
Created April 22, 2021 17:28
Show Gist options
  • Save MoAlyousef/b48ac10bef08c63f318513d216c9b346 to your computer and use it in GitHub Desktop.
Save MoAlyousef/b48ac10bef08c63f318513d216c9b346 to your computer and use it in GitHub Desktop.
Example of Hover support for buttons, it basically requires handling the Event::Enter and Event::Leave events.
use fltk::{
app, button,
enums::{Color, Event, FrameType},
prelude::*,
window,
};
const COLOR: u32 = 0xB39DDB;
const SELECTION_COLOR: u32 = 0x9575CD;
const HOVER_COLOR: u32 = 0xD1C4E9;
fn main() {
let app = app::App::default();
app::set_visible_focus(false);
let mut win = window::Window::default().with_size(400, 300);
win.set_color(Color::White);
let mut but = button::Button::default()
.with_size(80, 40)
.with_label("Click!")
.center_of_parent();
but.set_color(Color::from_hex(COLOR));
but.set_selection_color(Color::from_hex(SELECTION_COLOR));
but.set_frame(FrameType::FlatBox);
win.end();
win.show();
but.handle(move |b, ev| match ev {
Event::Enter => {
b.set_color(Color::from_hex(HOVER_COLOR));
b.redraw();
true
}
Event::Leave => {
b.set_color(Color::from_hex(COLOR));
b.redraw();
true
}
_ => false,
});
but.set_callback(|_| println!("Clicked!"));
app.run().unwrap();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment