Skip to content

Commit ac430df

Browse files
authored
feat: add a separate action to cut the text (#12)
1 parent a911ba8 commit ac430df

File tree

7 files changed

+29
-16
lines changed

7 files changed

+29
-16
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "protextinator"
3-
version = "0.1.1"
3+
version = "0.2.0"
44
edition = "2021"
55
description = "Text management, made simple"
66
keywords = ["text", "rendering", "gui", "graphics", "image"]

src/action.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,9 @@ pub enum ActionResult {
4747
/// The text content was modified.
4848
TextChanged,
4949
/// Text should be inserted into the system clipboard.
50-
InsertToClipboard(String),
50+
TextCopied(String),
51+
/// Text should be inserted into the system clipboard, and the original text was cut.
52+
TextCut(String),
5153
/// Actions are disabled for this text state.
5254
ActionsDisabled,
5355
}

src/buffer_utils.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ pub(crate) fn update_buffer(
164164
font_system,
165165
text_style.font_size.value(),
166166
Some(buffer_measurement.x),
167-
text_style.wrap.unwrap_or_default().into(),
167+
wrap.into(),
168168
None,
169169
// TODO: what is the default tab width? Make it configurable?
170170
2,

src/state.rs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -278,10 +278,15 @@ impl<T> TextState<T> {
278278
pub fn set_text(&mut self, text: &str) {
279279
self.params.set_text(text);
280280

281+
// TODO: should we just reset cursor on whole text update?
281282
if self.cursor.byte_character_start > self.params.text_for_internal_use().len() {
282-
self.update_cursor_before_glyph_with_bytes_offset(
283-
self.params.text_for_internal_use().len(),
284-
);
283+
if text.is_empty() {
284+
self.cursor = ByteCursor::default()
285+
} else {
286+
self.update_cursor_before_glyph_with_bytes_offset(
287+
self.params.text_for_internal_use().len(),
288+
);
289+
}
285290
}
286291
}
287292

@@ -994,7 +999,7 @@ impl<T> TextState<T> {
994999

9951000
fn copy_selected_text(&mut self) -> ActionResult {
9961001
let selected_text = self.selected_text().unwrap_or("");
997-
ActionResult::InsertToClipboard(selected_text.to_string())
1002+
ActionResult::TextCopied(selected_text.to_string())
9981003
}
9991004

10001005
fn paste_text_at_cursor(&mut self, ctx: &mut TextContext, text: &str) -> ActionResult {
@@ -1018,7 +1023,7 @@ impl<T> TextState<T> {
10181023
let selected_text = self.selected_text().unwrap_or("").to_string();
10191024
self.remove_selected_text();
10201025
self.recalculate_with_update_reason(ctx, UpdateReason::DeletedTextAtCursor);
1021-
ActionResult::InsertToClipboard(selected_text)
1026+
ActionResult::TextCut(selected_text)
10221027
}
10231028

10241029
fn delete_selected_text_or_text_before_cursor(

src/style.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,12 @@ pub enum HorizontalTextAlignment {
375375
Justify,
376376
}
377377

378+
impl HorizontalTextAlignment {
379+
pub fn is_centered(&self) -> bool {
380+
matches!(self, HorizontalTextAlignment::Center)
381+
}
382+
}
383+
378384
impl From<HorizontalTextAlignment> for Option<Align> {
379385
/// Converts a `TextAlignment` to a [`cosmic_text::Align`] option.
380386
///

src/tests/copy_selected_text.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ pub fn test_copy_empty_selection() {
2323

2424
// Try to copy with no selection
2525
let result = text_state.apply_action(&mut ctx, &Action::CopySelectedText);
26-
assert!(matches!(result, ActionResult::InsertToClipboard(s) if s.is_empty()));
26+
assert!(matches!(result, ActionResult::TextCopied(s) if s.is_empty()));
2727
}
2828

2929
#[test]
@@ -51,7 +51,7 @@ pub fn test_copy_partial_selection() {
5151

5252
// Copy the selected text
5353
let result = text_state.apply_action(&mut ctx, &Action::CopySelectedText);
54-
assert!(matches!(result, ActionResult::InsertToClipboard(s) if s == "Hello"));
54+
assert!(matches!(result, ActionResult::TextCopied(s) if s == "Hello"));
5555
}
5656

5757
#[test]
@@ -77,8 +77,8 @@ pub fn test_copy_full_selection() {
7777
// Copy the selected text
7878
let result = text_state.apply_action(&mut ctx, &Action::CopySelectedText);
7979
match result {
80-
ActionResult::InsertToClipboard(s) => assert_eq!(s, "Hello World"),
81-
_ => panic!("Result is {result:?}, expected InsertToClipboard"),
80+
ActionResult::TextCopied(s) => assert_eq!(s, "Hello World"),
81+
_ => panic!("Result is {result:?}, expected TextCopied"),
8282
}
8383
}
8484

@@ -107,7 +107,7 @@ pub fn test_copy_cyrillic_text() {
107107

108108
// Copy the selected text
109109
let result = text_state.apply_action(&mut ctx, &Action::CopySelectedText);
110-
assert!(matches!(result, ActionResult::InsertToClipboard(s) if s == "Привет"));
110+
assert!(matches!(result, ActionResult::TextCopied(s) if s == "Привет"));
111111
}
112112

113113
#[test]
@@ -140,7 +140,7 @@ pub fn test_copy_after_editing() {
140140

141141
// Copy the selected text
142142
let result = text_state.apply_action(&mut ctx, &Action::CopySelectedText);
143-
assert!(matches!(result, ActionResult::InsertToClipboard(s) if s == "Test "));
143+
assert!(matches!(result, ActionResult::TextCopied(s) if s == "Test "));
144144
}
145145

146146
#[test]
@@ -199,5 +199,5 @@ pub fn test_copy_selection_from_middle() {
199199
let result = text_state.apply_action(&mut ctx, &Action::CopySelectedText);
200200

201201
// Verify the copy operation was successful and copied the correct text
202-
assert!(matches!(result, ActionResult::InsertToClipboard(s) if s == "bro"));
202+
assert!(matches!(result, ActionResult::TextCopied(s) if s == "bro"));
203203
}

0 commit comments

Comments
 (0)