Skip to content

Commit

Permalink
chore: rename highlight options, consistent highlight mode naming, ma…
Browse files Browse the repository at this point in the history
…ke match arms clearer
  • Loading branch information
DerpDays committed Jun 4, 2024
1 parent 2ad8f93 commit 94b5802
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 40 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ output-filename = "/tmp/test-%Y-%m-%d_%H:%M:%S.png"
save-after-copy = false
# Hide toolbars by default
default-hide-toolbars = false
# Whether to set block or line/pen as the default highlighter, other mode is accessible using CTRL.
default-block-highlight = true
# The primary highlighter to use, the other is accessible by holding CTRL at the start of a highlight [possible values: block, freehand]
primary-highlighter = "block"

# Font to use for text annotations
[font]
Expand Down Expand Up @@ -124,8 +124,8 @@ Options:
After copying the screenshot, save it to a file as well
-d, --default-hide-toolbars
Hide toolbars by default
--default-line-highlight
Change the default highlighter to the line/pen highlighter
--primary-highlighter <PRIMARY_HIGHLIGHTER>
The primary highlighter to use, secondary is accessible with CTRL [possible values: block, freehand]
--font-family <FONT_FAMILY>
Font family to use for text annotations
--font-style <FONT_STYLE>
Expand Down
4 changes: 2 additions & 2 deletions config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ output-filename = "/tmp/test-%Y-%m-%d_%H:%M:%S.png"
save-after-copy = false
# Hide toolbars by default
default-hide-toolbars = false
# Whether to set block or line/pen as the default highlighter, other mode is accessible using CTRL.
default-block-highlight = true
# The primary highlighter to use, the other is accessible by holding CTRL at the start of a highlight [possible values: block, freehand]
primary-highlighter = "block"

# Font to use for text annotations
[font]
Expand Down
11 changes: 9 additions & 2 deletions src/command_line.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ pub struct CommandLine {
#[arg(long)]
pub font_style: Option<String>,

/// Change the default highlighter to the line/pen highlighter.
/// The primary highlighter to use, secondary is accessible with CTRL.
#[arg(long)]
pub default_line_highlight: bool,
pub primary_highlighter: Option<Highlighters>,
}

#[derive(Debug, Clone, Copy, Default, ValueEnum)]
Expand All @@ -72,6 +72,13 @@ pub enum Tools {
Brush,
}

#[derive(Debug, Clone, Copy, Default, ValueEnum)]
pub enum Highlighters {
#[default]
Block,
Freehand,
}

impl std::fmt::Display for Tools {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
use Tools::*;
Expand Down
24 changes: 14 additions & 10 deletions src/configuration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ use serde_derive::Deserialize;
use thiserror::Error;
use xdg::{BaseDirectories, BaseDirectoriesError};

use crate::{command_line::CommandLine, style::Color, tools::Tools};
use crate::{
command_line::CommandLine,
style::Color,
tools::{Highlighters, Tools},
};

pub static APP_CONFIG: SharedState<Configuration> = SharedState::new();

Expand Down Expand Up @@ -39,7 +43,7 @@ pub struct Configuration {
color_palette: ColorPalette,
default_hide_toolbars: bool,
font: FontConfiguration,
default_block_highlight: bool,
primary_highlighter: Highlighters,
}

#[derive(Default)]
Expand Down Expand Up @@ -171,8 +175,8 @@ impl Configuration {
if let Some(v) = general.default_hide_toolbars {
self.default_hide_toolbars = v;
}
if let Some(v) = general.default_block_highlight {
self.default_block_highlight = v;
if let Some(v) = general.primary_highlighter {
self.primary_highlighter = v;
}
}
fn merge(&mut self, file: Option<ConfigurationFile>, command_line: CommandLine) {
Expand Down Expand Up @@ -224,8 +228,8 @@ impl Configuration {
self.font.style = Some(v);
}

if command_line.default_line_highlight {
self.default_block_highlight = !command_line.default_line_highlight;
if let Some(v) = command_line.primary_highlighter {
self.primary_highlighter = v.into();
}
}

Expand Down Expand Up @@ -269,8 +273,8 @@ impl Configuration {
self.default_hide_toolbars
}

pub fn default_block_highlight(&self) -> bool {
self.default_block_highlight
pub fn primary_highlighter(&self) -> Highlighters {
self.primary_highlighter
}
pub fn font(&self) -> &FontConfiguration {
&self.font
Expand All @@ -291,7 +295,7 @@ impl Default for Configuration {
color_palette: ColorPalette::default(),
default_hide_toolbars: false,
font: FontConfiguration::default(),
default_block_highlight: true,
primary_highlighter: Highlighters::Block,
}
}
}
Expand Down Expand Up @@ -335,7 +339,7 @@ struct ConfiguationFileGeneral {
output_filename: Option<String>,
save_after_copy: Option<bool>,
default_hide_toolbars: Option<bool>,
default_block_highlight: Option<bool>,
primary_highlighter: Option<Highlighters>,
}

#[derive(Deserialize)]
Expand Down
4 changes: 0 additions & 4 deletions src/style.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,8 +213,4 @@ impl Size {
Size::Large => 45.0 * size_factor,
}
}

pub fn default_block_highlight(self) -> bool {
APP_CONFIG.read().default_block_highlight()
}
}
62 changes: 45 additions & 17 deletions src/tools/highlight.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ use anyhow::Result;
use femtovg::{Paint, Path};

use relm4::gtk::gdk::{Key, ModifierType};
use serde_derive::Deserialize;

use crate::{
command_line,
configuration::APP_CONFIG,
math::{self, Vec2D},
sketch_board::{MouseEventMsg, MouseEventType},
Expand All @@ -17,14 +19,30 @@ use super::{Drawable, Tool, ToolUpdateResult};

const HIGHLIGHT_OPACITY: f64 = 0.4;

#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Copy, Hash, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum Highlighters {
Block = 0,
Freehand = 1,
}

impl From<command_line::Highlighters> for Highlighters {
fn from(tool: command_line::Highlighters) -> Self {
match tool {
command_line::Highlighters::Block => Self::Block,
command_line::Highlighters::Freehand => Self::Freehand,
}
}
}

#[derive(Clone, Debug)]
struct BlockHighlight {
top_left: Vec2D,
size: Option<Vec2D>,
}

#[derive(Clone, Debug)]
struct LineHighlight {
struct FreehandHighlight {
points: Vec<Vec2D>,
shift_pressed: bool,
}
Expand All @@ -39,7 +57,7 @@ trait Highlight {
fn highlight(&self, canvas: &mut femtovg::Canvas<femtovg::renderer::OpenGl>) -> Result<()>;
}

impl Highlight for Highlighter<LineHighlight> {
impl Highlight for Highlighter<FreehandHighlight> {
fn highlight(&self, canvas: &mut femtovg::Canvas<femtovg::renderer::OpenGl>) -> Result<()> {
canvas.save();

Expand Down Expand Up @@ -98,7 +116,7 @@ impl Highlight for Highlighter<BlockHighlight> {
#[derive(Clone, Debug)]
enum HighlightKind {
Block(Highlighter<BlockHighlight>),
Line(Highlighter<LineHighlight>),
Freehand(Highlighter<FreehandHighlight>),
}

#[derive(Default, Clone, Debug)]
Expand All @@ -115,7 +133,7 @@ impl Drawable for HighlightKind {
) -> Result<()> {
match self {
HighlightKind::Block(highlighter) => highlighter.highlight(canvas),
HighlightKind::Line(highlighter) => highlighter.highlight(canvas),
HighlightKind::Freehand(highlighter) => highlighter.highlight(canvas),
}
}
}
Expand All @@ -124,11 +142,17 @@ impl Tool for HighlightTool {
fn handle_mouse_event(&mut self, event: MouseEventMsg) -> ToolUpdateResult {
let shift_pressed = event.modifier.intersects(ModifierType::SHIFT_MASK);
let ctrl_pressed = event.modifier.intersects(ModifierType::CONTROL_MASK);
let default_highlight_block = APP_CONFIG.read().default_block_highlight();
let primary_highlighter = APP_CONFIG.read().primary_highlighter();
match event.type_ {
MouseEventType::BeginDrag => {
match (ctrl_pressed, default_highlight_block) {
(false, true) | (true, false) => {
// There exists two types of highlighting modes currently: freehand, block
// A user may set a primary highlighter mode, with the other being accessible
// by clicking CTRL when starting a highlight (doesn't need to be held).
match (primary_highlighter, ctrl_pressed) {
// This matches when CTRL is not pressed and the primary highlighting mode
// is block, along with its inverse, CTRL pressed with the freehand mode
// being their primary highlighting mode.
(Highlighters::Block, false) | (Highlighters::Freehand, true) => {
self.highlighter =
Some(HighlightKind::Block(Highlighter::<BlockHighlight> {
data: BlockHighlight {
Expand All @@ -138,14 +162,18 @@ impl Tool for HighlightTool {
style: self.style,
}))
}
(false, false) | (true, true) => {
self.highlighter = Some(HighlightKind::Line(Highlighter::<LineHighlight> {
data: LineHighlight {
points: vec![event.pos],
shift_pressed,
},
style: self.style,
}))
// This matches the remaining two cases, which is when the user has the
// freehand mode as the primary mode and CTRL is not pressed, and conversely,
// when CTRL is pressed and the users primary mode is block.
(Highlighters::Freehand, false) | (Highlighters::Block, true) => {
self.highlighter =
Some(HighlightKind::Freehand(Highlighter::<FreehandHighlight> {
data: FreehandHighlight {
points: vec![event.pos],
shift_pressed,
},
style: self.style,
}))
}
}

Expand All @@ -169,7 +197,7 @@ impl Tool for HighlightTool {
};
ToolUpdateResult::Redraw
}
HighlightKind::Line(highlighter) => {
HighlightKind::Freehand(highlighter) => {
if event.pos == Vec2D::zero() {
return ToolUpdateResult::Unmodified;
};
Expand Down Expand Up @@ -234,7 +262,7 @@ impl Tool for HighlightTool {
// add an extra point when shift is unheld, this allows for users to make sharper turns.
// press (aka: release) shift a second time to remove the added point.
if event.key == Key::Shift_L || event.key == Key::Shift_R {
if let Some(HighlightKind::Line(highlighter)) = &mut self.highlighter {
if let Some(HighlightKind::Freehand(highlighter)) = &mut self.highlighter {
let points = &mut highlighter.data.points;
let last = points
.last()
Expand Down
2 changes: 1 addition & 1 deletion src/tools/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ pub enum ToolUpdateResult {
pub use arrow::ArrowTool;
pub use blur::BlurTool;
pub use crop::CropTool;
pub use highlight::HighlightTool;
pub use highlight::{HighlightTool, Highlighters};
pub use line::LineTool;
pub use rectangle::RectangleTool;
pub use text::TextTool;
Expand Down

0 comments on commit 94b5802

Please sign in to comment.