Skip to content

Instantly share code, notes, and snippets.

@MadameMinty
Last active April 22, 2023 21:17
Show Gist options
  • Save MadameMinty/7c9d8d0114d1e2316a5146902d170be4 to your computer and use it in GitHub Desktop.
Save MadameMinty/7c9d8d0114d1e2316a5146902d170be4 to your computer and use it in GitHub Desktop.
Rust: FLTK: Generic window, borderless, rounded
[package]
name = "generic_window"
version = "0.1.0"
edition = "2021"
[[bin]]
name = "generic_window"
path = "src/generic_window.rs"
[dependencies]
fltk = "1.4.0"
#![windows_subsystem = "windows"]
use fltk::{
app,
enums::{Color, Event, Key},
image::RgbImage,
prelude::{
WidgetExt, WindowExt, GroupExt,
SurfaceDevice, WidgetBase},
window::Window,
surface::ImageSurface,
draw,
};
fn main() {
let rounding_radius: i32 = 30;
let window_size_x: i32 = 200;
let window_size_y: i32 = 200;
let window_shape_image = prep_shape(window_size_x, window_size_y, rounding_radius);
let app = app::App::default();
// window
let mut win = Window::default()
.with_size(window_size_x, window_size_y)
.with_label("Test")
.center_screen();
win.set_shape(Some(window_shape_image));
// win.set_border(false); // set_shape already does this
win.end();
win.show();
// quit on escape, unfocus
win.handle({
move |_wself, event| match event {
Event::KeyDown => {
if app::event_key() == Key::Escape {
app.quit();
true
} else {
false
}
},
Event::Unfocus => {
app.quit();
true
},
_ => false
}
});
app.run().unwrap();
}
fn prep_shape(w: i32, h: i32, r: i32) -> RgbImage {
// rounded rectangle
let surface = ImageSurface::new(w, h, false);
ImageSurface::push_current(&surface);
// transparent
draw::set_draw_color(Color::Black);
draw::draw_rectf(-1, -1, w + 2, h + 2);
// visible
draw::set_draw_color(Color::White);
draw::draw_rounded_rectf(0, 0, w, h, r);
let img = surface.image().unwrap();
ImageSurface::pop_current();
return img;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment