Skip to content

Instantly share code, notes, and snippets.

@pa-0
Forked from MoAlyousef/hover_button.rs
Created August 25, 2025 21:15
Show Gist options
  • Save pa-0/31c35767ad0eb7245e067ae852edd138 to your computer and use it in GitHub Desktop.
Save pa-0/31c35767ad0eb7245e067ae852edd138 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