Skip to content

Commit

Permalink
Split binding icon buttons to responses from building icon buttons
Browse files Browse the repository at this point in the history
  • Loading branch information
ctrlcctrlv committed Mar 28, 2023
1 parent 3105fe8 commit 87b5dfa
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 59 deletions.
3 changes: 3 additions & 0 deletions src/user_interface/gui/icons/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
mod button;
pub use self::button::build as build_button;

pub const _KNIFE: &str = "\u{F000}";
pub const MEASURE: &str = "\u{F001}";
pub const PAN: &str = "\u{F002}";
Expand Down
8 changes: 4 additions & 4 deletions src/user_interface/gui/mod.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
pub mod icons;
pub use self::icons::build_button as build_icon_button;
pub mod menu_bar;
#[macro_use]
pub(crate) mod msgbox;
pub(crate) use self::msgbox::gui_error as error;
pub mod prompts;
pub mod textedit_buffer;
pub mod tool_bar;
pub mod window;
pub mod windows;

#[macro_use]
pub(crate) mod msgbox;

pub(crate) use self::msgbox::gui_error as error;
use self::window::{GlifWindow, WindowManager};
use super::{egui_manager::EguiManager, Interface};
use crate::editor::Editor;
Expand Down
66 changes: 11 additions & 55 deletions src/user_interface/gui/tool_bar.rs
Original file line number Diff line number Diff line change
@@ -1,65 +1,21 @@
use egui::{Align2, Color32, Context, Stroke, Ui};
use lazy_static::lazy_static;
use egui::{Align2, Context, Ui};
use crate::{constants::*, editor::Editor, tools::ToolEnum, user_interface::{Interface, gui::build_icon_button}};

use super::super::icons;
use super::icons;

use crate::{constants::*, editor::Editor, tools::ToolEnum, user_interface::Interface};

pub fn build_button(v: &mut Editor, ui: &mut Ui, text: &str, te: ToolEnum) {
let stroke = if v.get_tool() == te {
Stroke {
width: 2.0,
color: Color32::from_rgb(255, 190, 0),
}
} else {
Stroke::NONE
};

let use_button_font = text
.chars()
.nth(0)
.map(u32::from)
.map(|c| c >= 0xF000 && c <= 0xF037)
.unwrap_or(false);
let icons_font_family = egui::FontFamily::Name("icons".into());
let icons_font_id = egui::FontId {
family: icons_font_family.clone(),
..Default::default()
};
let size = ui.fonts(|f| f.row_height(&icons_font_id));
let EGUI_DEFAULT_BUTTON_FONT_ID: egui::FontId = {
let s = ui.style_mut();
s.text_styles.get(&egui::TextStyle::Button).unwrap().clone()
};
if use_button_font {
let s = ui.style_mut();
s.text_styles.insert(
egui::TextStyle::Button,
egui::FontId {
family: icons_font_family,
size,
},
);
}
{
let button = egui::Button::new(text)
.stroke(stroke)
.min_size(egui::vec2(22. * FONT_SCALE_FACTOR, 30. * FONT_SCALE_FACTOR));

let response = ui.add(button);
fn build_button<'a>(v: &mut Editor, ui: &mut Ui, text: &str, te: ToolEnum) {
// Must build button outside bind_response so that v can move into the closure.
let b = build_icon_button(v, ui, text, te);
let mut bind_response = |response: egui::Response| {
if response.clicked() {
v.set_tool(te);

response.on_hover_text(format!("{:?}", te));
response.on_hover_text(format!("{:?}", te.to_string()));
}
}
if use_button_font {
let s = ui.style_mut();
*(s.text_styles.get_mut(&egui::TextStyle::Button).unwrap()) = EGUI_DEFAULT_BUTTON_FONT_ID;
let s = s.to_owned();
ui.set_style(s.to_owned());
}
};
bind_response(b)
}

pub fn tool_bar(ctx: &Context, v: &mut Editor, _i: &mut Interface) {
egui::Window::new("Tools")
.anchor(Align2::LEFT_TOP, [16., 31. * FONT_SCALE_FACTOR])
Expand Down

0 comments on commit 87b5dfa

Please sign in to comment.