::get().with(move |model| model.clone());
- let window = app::first_window().unwrap();
- fs::write(
- file,
- [
- (window.width() / U8) as u8, //[0]
- (window.width() % U8) as u8, //[1]
- (window.height() / U8) as u8, //[2]
- (window.height() % U8) as u8, //[3]
- self.from, //[4]
- self.to, //[5]
- self.font, //[6]
- self.size, //[7]
- ],
- )
- .unwrap();
- app::quit();
- }
-}
-
-fn button(tooltip: &str, label: &str, flex: &mut Flex) -> Button {
- let mut element = Button::default().with_label(label).with_id(tooltip);
- element.set_tooltip(tooltip);
- element.set_label_size(HEIGHT / 2);
- flex.fixed(&element, HEIGHT);
- element
-}
-
-fn counter(tooltip: &str, value: f64, flex: &mut Flex) -> Counter {
- let mut element = Counter::default();
- element.set_tooltip(tooltip);
- element.set_range(14_f64, 22_f64);
- element.set_precision(0);
- element.set_value(value);
- flex.fixed(&element, WIDTH - HEIGHT);
- element
-}
-
-fn dial(flex: &mut Flex) {
- const DIAL: u8 = 120;
- let mut element = Dial::default().with_id("SPINNER");
- element.deactivate();
- element.set_maximum((DIAL / 4 * 3) as f64);
- element.set_value(element.minimum());
- element.set_callback(move |dial| {
- dial.set_value(if dial.value() == (DIAL - 1) as f64 {
- dial.minimum()
- } else {
- dial.value() + 1f64
- })
- });
- flex.fixed(&element, HEIGHT);
-}
-
-fn choice(tooltip: &str, choice: &str, value: u8, flex: &mut Flex) -> Choice {
- let mut element = Choice::default();
- element.set_tooltip(tooltip);
- element.add_choice(choice);
- element.set_value(value as i32);
- flex.fixed(&element, WIDTH);
- element
-}
-
-fn text(tooltip: &str, value: &str, font: u8, size: u8) -> TextEditor {
- let mut element = TextEditor::default().with_id(tooltip);
- element.set_tooltip(tooltip);
- element.set_linenumber_width(HEIGHT);
- element.set_buffer(TextBuffer::default());
- element.wrap_mode(WrapMode::AtBounds, 0);
- element.buffer().unwrap().set_text(value);
- element.set_color(Color::from_hex(0x002b36));
- element.set_text_color(Color::from_hex(0x93a1a1));
- element.set_text_font(Font::by_index(font as usize));
- element.set_text_size(size as i32);
- element
-}
-
-fn menu(flex: &mut Flex) {
- let element = MenuButton::default();
- element
- .clone()
- .on_item_event(
- "@#circle T&ranslate",
- Shortcut::Ctrl | 'r',
- MenuFlag::Normal,
- move |_| Message::Translate,
- )
- .on_item_event(
- "@#search &Info",
- Shortcut::Ctrl | 'i',
- MenuFlag::Normal,
- move |_| Message::Info,
- )
- .on_item_event(
- "@#1+ &Quit",
- Shortcut::Ctrl | 'q',
- MenuFlag::Normal,
- move |_| Message::Quit,
- );
- flex.fixed(&element, HEIGHT);
-}
-
-fn info() {
- const INFO: &str = r#"
-FlDialect
- is similar to
- Dialect
- written using
- FLTK-RS
-
"#;
- let mut dialog = HelpDialog::default();
- dialog.set_value(INFO);
- dialog.set_text_size(16);
- dialog.show();
- while dialog.shown() {
- app::wait();
- }
-}
-
-pub fn run(voice: bool, from: String, to: String, word: String) -> String {
- let run = Command::new("trans")
- .args([
- "-join-sentence",
- "-no-ansi",
- "-show-languages",
- "n",
- "-show-original",
- "n",
- "-show-original-dictionary",
- "n",
- "-show-original-dictionary",
- "n",
- "-show-prompt-message",
- "n",
- "-show-alternatives",
- "n",
- "-show-translation-phonetics",
- "n",
- "-indent",
- "2",
- "-source",
- &from,
- "-target",
- &to,
- match word.split_whitespace().count() {
- 1 => "",
- _ => "-brief",
- },
- if voice { "-speak" } else { "" },
- &word.trim().replace("\n\n", "\n"),
- ])
- .output()
- .expect("failed to execute bash");
- String::from_utf8_lossy(match run.status.success() {
- true => &run.stdout,
- false => &run.stderr,
- })
- .to_string()
-}
-
-pub fn list() -> Vec {
- if cfg!(target_family = "unix") {
- let run = Command::new("trans")
- .arg("-list-languages-english")
- .output()
- .expect("failed to execute bash");
- match run.status.success() {
- true => String::from_utf8_lossy(&run.stdout)
- .lines()
- .map(str::to_string)
- .collect::>(),
- false => panic!("\x1b[31m{}\x1b[0m", String::from_utf8_lossy(&run.stderr)),
- }
- } else {
- Vec::from([String::from("no way")])
- }
-}
-
-pub fn once() -> bool {
- if cfg!(target_os = "linux") {
- let run = Command::new("lsof")
- .args(["-t", env::current_exe().unwrap().to_str().unwrap()])
- .output()
- .expect("failed to execute bash");
- match run.status.success() {
- true => {
- String::from_utf8_lossy(&run.stdout)
- .split_whitespace()
- .count()
- == 1
- }
- false => panic!("\x1b[31m{}\x1b[0m", String::from_utf8_lossy(&run.stderr)),
- }
- } else {
- true
- }
-}
-
-const NAME: &str = "FlDialect";
-const PATH: &str = "/.config";
-const PAD: i32 = 10;
-const HEIGHT: i32 = PAD * 3;
-const WIDTH: i32 = 125;
-const U8: i32 = 255;
-const DEFAULT: [u8; 9] = [
- 1, // [0] window_width * U8 +
- 105, // [1] window_width_fract
- 2, // [2] window_height * U8 +
- 130, // [3] window_height_fract
- 0, // [4] theme
- 119, // [5] header_from
- 35, // [6] header_to
- 1, // [7] footer_font
- 14, // [8] footer_size
-];
diff --git a/examples/flglyph.rs b/examples/flglyph.rs
index 40451c6..44fa53a 100644
--- a/examples/flglyph.rs
+++ b/examples/flglyph.rs
@@ -2,11 +2,11 @@
use flemish::{
app,
+ browser::{Browser, BrowserType},
button::Button,
color_themes,
- enums::{CallbackTrigger, FrameType, Font},
+ enums::{CallbackTrigger, Font, FrameType},
frame::Frame,
- browser::{Browser, BrowserType},
group::Flex,
prelude::*,
OnEvent, Sandbox, Settings,
@@ -21,7 +21,9 @@ struct Model {
impl Model {
fn init() -> Self {
Self {
- list: (0x2700..=0x27BF).map(|x| char::from_u32(x).unwrap()).collect(),
+ list: (0x2700..=0x27BF)
+ .map(|x| char::from_u32(x).unwrap())
+ .collect(),
curr: 0,
}
}
@@ -49,10 +51,7 @@ impl Model {
pub fn main() {
Model::new().run(Settings {
size: (640, 360),
- resizable: false,
- ignore_esc_close: true,
color_map: Some(color_themes::DARK_THEME),
- scheme: Some(app::Scheme::Base),
..Default::default()
})
}
@@ -65,7 +64,7 @@ const WIDTH: i32 = HEIGHT * 3;
enum Message {
Inc,
Dec,
- Choice(usize)
+ Choice(usize),
}
impl Sandbox for Model {
@@ -87,8 +86,12 @@ impl Sandbox for Model {
Message::Choice((browser.value() as usize).saturating_sub(1))
});
let mut buttons = Flex::default();
- Button::default().with_label("@#<").on_event(move |_| Message::Dec);
- Button::default().with_label("@#>").on_event(move |_| Message::Inc);
+ Button::default()
+ .with_label("@#<")
+ .on_event(move |_| Message::Dec);
+ Button::default()
+ .with_label("@#>")
+ .on_event(move |_| Message::Inc);
buttons.end();
buttons.set_pad(0);
left.end();
diff --git a/examples/flnetport.rs b/examples/flnetport.rs
index 081538e..155d95e 100644
--- a/examples/flnetport.rs
+++ b/examples/flnetport.rs
@@ -24,10 +24,7 @@ use {
pub fn main() {
Model::new().run(Settings {
size: (640, 360),
- resizable: false,
- ignore_esc_close: true,
color_map: Some(color_themes::DARK_THEME),
- scheme: Some(app::Scheme::Base),
..Default::default()
})
}
diff --git a/examples/flpage.rs b/examples/flpage.rs
index 97ed86c..02c814d 100644
--- a/examples/flpage.rs
+++ b/examples/flpage.rs
@@ -8,10 +8,7 @@ use flemish::{
pub fn main() {
MyApp::new().run(Settings {
size: (300, 180),
- resizable: true,
- ignore_esc_close: true,
color_map: Some(color_themes::BLACK_THEME),
- scheme: Some(app::Scheme::Base),
..Default::default()
})
}
diff --git a/examples/flpicture.rs b/examples/flpicture.rs
index 21c4f01..26c90ba 100644
--- a/examples/flpicture.rs
+++ b/examples/flpicture.rs
@@ -18,10 +18,7 @@ use std::fs;
pub fn main() {
Model::new().run(Settings {
size: (640, 360),
- resizable: false,
- ignore_esc_close: true,
color_map: Some(color_themes::DARK_THEME),
- scheme: Some(app::Scheme::Base),
..Default::default()
})
}
diff --git a/examples/inputchoice.rs b/examples/inputchoice.rs
index 98befdb..2d7468c 100644
--- a/examples/inputchoice.rs
+++ b/examples/inputchoice.rs
@@ -8,10 +8,7 @@ use flemish::{
pub fn main() {
Model::new().run(Settings {
size: (360, 640),
- resizable: false,
- ignore_esc_close: true,
color_map: Some(color_themes::DARK_THEME),
- scheme: Some(app::Scheme::Base),
..Default::default()
})
}
diff --git a/examples/menu.rs b/examples/menu.rs
index ceaf7c1..fa20458 100644
--- a/examples/menu.rs
+++ b/examples/menu.rs
@@ -11,10 +11,7 @@ use flemish::{
pub fn main() {
Model::new().run(Settings {
size: (640, 360),
- resizable: true,
- ignore_esc_close: true,
color_map: Some(color_themes::DARK_THEME),
- scheme: Some(app::Scheme::Base),
..Default::default()
})
}
diff --git a/examples/progress.rs b/examples/progress.rs
index 80761a3..792d1d8 100644
--- a/examples/progress.rs
+++ b/examples/progress.rs
@@ -12,10 +12,7 @@ use flemish::{
pub fn main() {
Model::new().run(Settings {
size: (640, 360),
- resizable: false,
- ignore_esc_close: true,
color_map: Some(color_themes::DARK_THEME),
- scheme: Some(app::Scheme::Base),
..Default::default()
})
}
diff --git a/examples/temperature.rs b/examples/temperature.rs
index cb56d54..cdcc772 100644
--- a/examples/temperature.rs
+++ b/examples/temperature.rs
@@ -11,10 +11,7 @@ use flemish::{
pub fn main() {
Model::new().run(Settings {
size: (640, 360),
- resizable: false,
- ignore_esc_close: true,
color_map: Some(color_themes::DARK_THEME),
- scheme: Some(app::Scheme::Base),
..Default::default()
})
}
diff --git a/src/lib.rs b/src/lib.rs
index 465fa40..d437539 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -11,7 +11,7 @@ where
{
fn on_event T>(self, cb: F) -> Self;
fn set_activate(self, flag: bool) -> Self;
- fn set_visible(self, flag: bool) -> Self;
+ fn set_visible(self, flag: bool) -> Self;
}
pub trait OnMenuEvent
@@ -40,18 +40,16 @@ where
self
}
fn set_activate(mut self, flag: bool) -> Self {
- if flag {
- self.activate();
- } else {
- self.deactivate();
+ match flag {
+ true => self.activate(),
+ false => self.deactivate(),
}
self
}
fn set_visible(mut self, flag: bool) -> Self {
- if flag {
- self.show();
- } else {
- self.hide();
+ match flag {
+ true => self.show(),
+ false => self.hide(),
}
self
}
@@ -88,11 +86,12 @@ pub struct Settings {
pub inactive: Option,
pub selection: Option,
pub font: Option,
+ pub xclass: Option,
+ pub icon: Option,
pub font_size: u8,
pub scheme: Option,
pub color_map: Option<&'static [fltk_theme::ColorMap]>,
pub theme: Option,
- pub ignore_esc_close: bool,
pub size_range: Option<(i32, i32, i32, i32)>,
pub on_close_fn: Option>,
}
@@ -105,15 +104,18 @@ pub trait Sandbox {
fn update(&mut self, message: Self::Message);
fn run(&mut self, settings: Settings) {
let a = app::App::default();
- let color_theme = if let Some(color_map) = settings.color_map {
+ if let Some(scheme) = settings.scheme {
+ app::set_scheme(scheme);
+ } else {
+ app::set_scheme(app::Scheme::Base);
+ }
+ if let Some(color_map) = settings.color_map {
fltk_theme::ColorTheme::from_colormap(color_map)
} else {
- fltk_theme::ColorTheme::from_colormap(fltk_theme::color_themes::BLACK_THEME)
- };
- color_theme.apply();
+ fltk_theme::ColorTheme::from_colormap(fltk_theme::color_themes::GRAY_THEME)
+ }.apply();
if let Some(theme) = settings.theme {
- let widget_theme = fltk_theme::WidgetTheme::new(theme);
- widget_theme.apply();
+ fltk_theme::WidgetTheme::new(theme).apply();
}
if let Some(color) = settings.background {
let c = color.to_rgb();
@@ -135,43 +137,49 @@ pub trait Sandbox {
let c = color.to_rgb();
app::set_inactive_color(c.0, c.1, c.2);
}
- if settings.font_size != 0 {
- app::set_font_size(settings.font_size);
- }
- if let Some(scheme) = settings.scheme {
- app::set_scheme(scheme);
- } else {
- app::set_scheme(app::Scheme::Gtk);
- }
if let Some(font) = settings.font {
app::set_font(font);
}
+ if settings.font_size != 0 {
+ app::set_font_size(settings.font_size);
+ }
let (w, h) = settings.size;
let w = if w == 0 { 400 } else { w };
let h = if h == 0 { 300 } else { h };
- let (x, y) = settings.pos;
+ let (mut x, mut y) = settings.pos;
+ if (x, y) == (0, 0) {
+ x = ((app::screen_size().0 - w as f64) / 2.0) as i32;
+ y = ((app::screen_size().1 - h as f64) / 2.0) as i32;
+ }
let mut win = window::Window::default()
.with_size(w, h)
+ .with_pos(x, y)
.with_label(&self.title());
- if (x, y) != (0, 0) {
- win.set_pos(x, y);
+ if let Some(value) = settings.xclass {
+ win.set_xclass(&value);
}
- if let Some((min_w, min_h, max_w, max_h)) = settings.size_range {
- win.size_range(min_w, min_h, max_w, max_h);
+ if let Some(value) = settings.icon {
+ win.set_icon(Some(value));
}
- if settings.ignore_esc_close {
+ if let Some(close_fn) = settings.on_close_fn {
+ win.set_callback(close_fn);
+ } else {
win.set_callback(move |_| {
if app::event() == enums::Event::Close {
- app::quit();
+ app::quit();
}
});
}
- if let Some(close_fn) = settings.on_close_fn {
- win.set_callback(close_fn);
- }
self.view();
win.end();
- win.make_resizable(settings.resizable);
+ if settings.resizable {
+ win.make_resizable(settings.resizable);
+ if let Some((min_w, min_h, max_w, max_h)) = settings.size_range {
+ win.size_range(min_w, min_h, max_w, max_h);
+ } else {
+ win.size_range(w, h, 0, 0);
+ }
+ };
win.show();
let (_, r) = app::channel::();
while a.wait() {
@@ -179,6 +187,7 @@ pub trait Sandbox {
self.update(msg);
win.clear();
win.begin();
+ win.set_label(&self.title());
self.view();
win.end();
app::redraw();