Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/assets/default.css
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,7 @@

.overlay .toolbar-bottom { border-radius: 6px 6px 0px 0px; }
.overlay .toolbar-top { border-radius: 0px 0px 6px 6px; }

button.editing {
color: @accent_color;
}
9 changes: 9 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ enum AppInput {
ScaleFactorChanged,
FullscreenChanged(bool),
DimensionsUpdate(Option<(i32, i32)>),
ToolEditingChanged(bool),
}

#[derive(Debug)]
Expand Down Expand Up @@ -289,6 +290,11 @@ impl Component for App {
.sender()
.emit(StyleToolbarInput::DimensionsChanged(d));
}
AppInput::ToolEditingChanged(editing) => {
self.tools_toolbar
.sender()
.emit(ToolsToolbarInput::SetToolEditing(editing));
}
}
}

Expand Down Expand Up @@ -326,6 +332,9 @@ impl Component for App {
SketchBoardOutput::DimensionsUpdate(dimensions) => {
AppInput::DimensionsUpdate(dimensions)
}
SketchBoardOutput::ToolEditingChanged(editing) => {
AppInput::ToolEditingChanged(editing)
}
});

// Toolbars
Expand Down
12 changes: 12 additions & 0 deletions src/sketch_board.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ pub enum SketchBoardOutput {
ToolSwitchShortcut(Tools),
ColorSwitchShortcut(u64),
DimensionsUpdate(Option<(i32, i32)>),
ToolEditingChanged(bool),
}

#[derive(Debug, Clone)]
Expand Down Expand Up @@ -239,6 +240,7 @@ impl InputEvent {
pub struct SketchBoard {
renderer: FemtoVGArea,
active_tool: Rc<RefCell<dyn Tool>>,
tool_edit_mode: bool,
tools: ToolsManager,
style: Style,
im_context: gtk::IMMulticontext,
Expand Down Expand Up @@ -974,6 +976,7 @@ impl Component for SketchBoard {

fn update(&mut self, msg: SketchBoardInput, sender: ComponentSender<Self>, _root: &Self::Root) {
// handle resize ourselves, pass everything else to tool
let sender_clone = sender.clone();
let result = match msg {
SketchBoardInput::InputEvent(mut ie) => {
if let InputEvent::Key(ke) = ie {
Expand Down Expand Up @@ -1142,6 +1145,14 @@ impl Component for SketchBoard {
}
};

let editing = self.active_tool.borrow().active();
if editing != self.tool_edit_mode {
self.tool_edit_mode = editing;
sender_clone
.output_sender()
.emit(SketchBoardOutput::ToolEditingChanged(editing));
}

// println!(" Result={:?}", result);
match result {
ToolUpdateResult::Commit(drawable) => {
Expand Down Expand Up @@ -1171,6 +1182,7 @@ impl Component for SketchBoard {
let mut model = Self {
renderer: FemtoVGArea::default(),
active_tool: tools.get(&config.initial_tool()),
tool_edit_mode: false,
style: Style::default(),
tools,
im_context,
Expand Down
20 changes: 17 additions & 3 deletions src/tools/arrow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ impl Tool for ArrowTool {
self.input_enabled = value;
}

fn active(&self) -> bool {
self.arrow.is_some()
}

fn get_tool_type(&self) -> super::Tools {
Tools::Arrow
}
Expand Down Expand Up @@ -107,9 +111,19 @@ impl Tool for ArrowTool {
}

fn handle_key_event(&mut self, event: crate::sketch_board::KeyEventMsg) -> ToolUpdateResult {
if event.key == Key::Escape && self.arrow.is_some() {
self.arrow = None;
ToolUpdateResult::Redraw
if let Some(arrow) = &self.arrow {
match event.key {
Key::Escape => {
self.arrow = None;
ToolUpdateResult::Redraw
}
Key::Return if arrow.end.is_some() => {
let result = arrow.clone_box();
self.arrow = None;
ToolUpdateResult::Commit(result)
}
_ => ToolUpdateResult::Unmodified,
}
} else {
ToolUpdateResult::Unmodified
}
Expand Down
21 changes: 18 additions & 3 deletions src/tools/blur.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,10 @@ impl Tool for BlurTool {
self.input_enabled = value;
}

fn active(&self) -> bool {
self.blur.is_some()
}

fn get_tool_type(&self) -> super::Tools {
Tools::Blur
}
Expand Down Expand Up @@ -227,9 +231,20 @@ impl Tool for BlurTool {
}

fn handle_key_event(&mut self, event: crate::sketch_board::KeyEventMsg) -> ToolUpdateResult {
if event.key == Key::Escape && self.blur.is_some() {
self.blur = None;
ToolUpdateResult::Redraw
if let Some(blur) = &mut self.blur {
match event.key {
Key::Escape => {
self.blur = None;
ToolUpdateResult::Redraw
}
Key::Return if blur.size.is_some() => {
blur.editing = false;
let result = blur.clone_box();
self.blur = None;
ToolUpdateResult::Commit(result)
}
_ => ToolUpdateResult::Unmodified,
}
} else {
ToolUpdateResult::Unmodified
}
Expand Down
8 changes: 8 additions & 0 deletions src/tools/crop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,14 @@ impl CropTool {
}

impl Tool for CropTool {
fn active(&self) -> bool {
if let Some(c) = &self.crop {
c.active
} else {
false
}
}

fn input_enabled(&self) -> bool {
self.input_enabled
}
Expand Down
21 changes: 18 additions & 3 deletions src/tools/ellipse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,10 @@ impl Tool for EllipseTool {
self.input_enabled = value;
}

fn active(&self) -> bool {
self.ellipse.is_some()
}

fn get_tool_type(&self) -> super::Tools {
Tools::Ellipse
}
Expand Down Expand Up @@ -182,9 +186,20 @@ impl Tool for EllipseTool {
}

fn handle_key_event(&mut self, event: crate::sketch_board::KeyEventMsg) -> ToolUpdateResult {
if event.key == Key::Escape && self.ellipse.is_some() {
self.ellipse = None;
ToolUpdateResult::Redraw
if let Some(ellipse) = &mut self.ellipse {
match event.key {
Key::Escape => {
self.ellipse = None;
ToolUpdateResult::Redraw
}
Key::Return if ellipse.radii.is_some() => {
ellipse.finishing = true;
let result = ellipse.clone_box();
self.ellipse = None;
ToolUpdateResult::Commit(result)
}
_ => ToolUpdateResult::Unmodified,
}
} else {
ToolUpdateResult::Unmodified
}
Expand Down
20 changes: 17 additions & 3 deletions src/tools/highlight.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,10 @@ impl Tool for HighlightTool {
self.input_enabled = value;
}

fn active(&self) -> bool {
self.highlighter.is_some()
}

fn get_tool_type(&self) -> super::Tools {
Tools::Highlight
}
Expand Down Expand Up @@ -296,9 +300,19 @@ impl Tool for HighlightTool {
}

fn handle_key_event(&mut self, event: crate::sketch_board::KeyEventMsg) -> ToolUpdateResult {
if event.key == Key::Escape && self.highlighter.is_some() {
self.highlighter = None;
return ToolUpdateResult::Redraw;
if self.highlighter.is_some() {
match event.key {
Key::Escape => {
self.highlighter = None;
return ToolUpdateResult::Redraw;
}
Key::Return => {
let result = self.highlighter.as_ref().unwrap().clone_box();
self.highlighter = None;
return ToolUpdateResult::Commit(result);
}
_ => {}
}
}
ToolUpdateResult::Unmodified
}
Expand Down
20 changes: 17 additions & 3 deletions src/tools/line.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ impl Tool for LineTool {
self.input_enabled = value;
}

fn active(&self) -> bool {
self.line.is_some()
}

fn handle_mouse_event(&mut self, event: MouseEventMsg) -> ToolUpdateResult {
match event.type_ {
MouseEventType::BeginDrag => {
Expand Down Expand Up @@ -125,9 +129,19 @@ impl Tool for LineTool {
}

fn handle_key_event(&mut self, event: crate::sketch_board::KeyEventMsg) -> ToolUpdateResult {
if event.key == Key::Escape && self.line.is_some() {
self.line = None;
ToolUpdateResult::Redraw
if let Some(line) = &self.line {
match event.key {
Key::Escape => {
self.line = None;
ToolUpdateResult::Redraw
}
Key::Return if line.direction.is_some() => {
let result = line.clone_box();
self.line = None;
ToolUpdateResult::Commit(result)
}
_ => ToolUpdateResult::Unmodified,
}
} else {
ToolUpdateResult::Unmodified
}
Expand Down
21 changes: 18 additions & 3 deletions src/tools/rectangle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,10 @@ impl Tool for RectangleTool {
self.input_enabled = value;
}

fn active(&self) -> bool {
self.rectangle.is_some()
}

fn handle_mouse_event(&mut self, event: MouseEventMsg) -> ToolUpdateResult {
match event.type_ {
MouseEventType::BeginDrag => {
Expand Down Expand Up @@ -176,9 +180,20 @@ impl Tool for RectangleTool {
}

fn handle_key_event(&mut self, event: crate::sketch_board::KeyEventMsg) -> ToolUpdateResult {
if event.key == Key::Escape && self.rectangle.is_some() {
self.rectangle = None;
ToolUpdateResult::Redraw
if let Some(rectangle) = &mut self.rectangle {
match event.key {
Key::Escape => {
self.rectangle = None;
ToolUpdateResult::Redraw
}
Key::Return if rectangle.size.is_some() => {
rectangle.finishing = true;
let result = rectangle.clone_box();
self.rectangle = None;
ToolUpdateResult::Commit(result)
}
_ => ToolUpdateResult::Unmodified,
}
} else {
ToolUpdateResult::Unmodified
}
Expand Down
17 changes: 17 additions & 0 deletions src/ui/toolbars.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ pub enum ToolsToolbarInput {
SetVisibility(bool),
ToggleVisibility,
SwitchSelectedTool(Tools),
SetToolEditing(bool),
}

#[derive(Debug, Copy, Clone)]
Expand Down Expand Up @@ -298,10 +299,22 @@ impl SimpleComponent for ToolsToolbar {
// Change state of action, let GTK update the UI
self.tool_action.change_state(&tool.to_variant());

if let Some(button) = self.active_button.as_ref() {
button.remove_css_class("editing");
}
if let Some(selected_tool_button) = self.tool_buttons.get(&tool) {
self.active_button = Some(selected_tool_button.clone());
}
}
ToolsToolbarInput::SetToolEditing(editing) => {
if let Some(button) = self.active_button.as_ref() {
if editing {
button.add_css_class("editing");
} else {
button.remove_css_class("editing");
}
}
}
}
}

Expand All @@ -319,6 +332,10 @@ impl SimpleComponent for ToolsToolbar {
sender_tmp
.output_sender()
.emit(ToolbarEvent::ToolSelected(*state));
// also change tracked active button
sender_tmp
.input_sender()
.emit(ToolsToolbarInput::SwitchSelectedTool(*state))
},
);

Expand Down
Loading