Persist the text selection when we change the font

This commit is contained in:
David Capello 2024-08-26 18:21:13 -03:00
parent 08de330123
commit 8fa4775def
4 changed files with 27 additions and 2 deletions

View File

@ -34,6 +34,7 @@
#include "ui/entry.h"
#include "ui/message.h"
#include "ui/paint_event.h"
#include "ui/system.h"
#ifdef LAF_SKIA
#include "app/util/shader_helpers.h"
@ -57,6 +58,8 @@ public:
// We have to draw the editor as background of this ui::Entry.
setTransparent(true);
setPersistSelection(true);
// TODO move this opacity() to Site class
int t, opacity = (site.layer()->isImage() ?
static_cast<LayerImage*>(site.layer())->opacity(): 255);
@ -300,6 +303,15 @@ bool WritingTextState::onKeyUp(Editor*, KeyMessage* msg)
return true;
}
void WritingTextState::onEditorGotFocus(Editor* editor)
{
// Focus the entry when we focus the editor, it happens when we
// change the font settings, so we keep the focus in the entry
// field.
if (m_entry)
m_entry->requestFocus();
}
void WritingTextState::onEditorResize(Editor* editor)
{
const gfx::PointF scale(editor->projection().scaleX(),
@ -404,6 +416,11 @@ void WritingTextState::onFontChange()
// This is useful to show changes to the anti-alias option
// immediately.
auto dummy = m_entry->extraCel();
ui::execute_from_ui_thread([this]{
if (m_entry)
m_entry->requestFocus();
});
}
}

View File

@ -24,6 +24,7 @@ namespace app {
LeaveAction onLeaveState(Editor* editor, EditorState* newState) override;
void onEnterState(Editor* editor) override;
void onBeforePopState(Editor* editor) override;
void onEditorGotFocus(Editor* editor) override;
void onEditorResize(Editor* editor) override;
bool onMouseDown(Editor* editor, ui::MouseMessage* msg) override;
bool onMouseUp(Editor* editor, ui::MouseMessage* msg) override;

View File

@ -51,6 +51,7 @@ Entry::Entry(const int maxsize, const char* format, ...)
, m_readonly(false)
, m_recent_focused(false)
, m_lock_selection(false)
, m_persist_selection(false)
, m_translate_dead_keys(true)
, m_scale(1.0f, 1.0f)
{
@ -273,7 +274,8 @@ bool Entry::onProcessMessage(Message* msg)
m_lock_selection = false;
}
else {
selectAllText();
if (!m_persist_selection)
selectAllText();
m_recent_focused = true;
}
@ -287,7 +289,7 @@ bool Entry::onProcessMessage(Message* msg)
stopTimer();
if (!m_lock_selection)
if (!m_lock_selection && !m_persist_selection)
deselectText();
m_recent_focused = false;

View File

@ -54,6 +54,10 @@ namespace ui {
std::string selectedText() const;
Range selectedRange() const;
// Set to true if you want to persists the selection when the
// keyboard focus is lost/re-enters.
void setPersistSelection(bool state) { m_persist_selection = state; }
void setSuffix(const std::string& suffix);
std::string getSuffix();
@ -141,6 +145,7 @@ namespace ui {
bool m_readonly : 1;
bool m_recent_focused : 1;
bool m_lock_selection : 1;
bool m_persist_selection : 1;
bool m_translate_dead_keys : 1;
Range m_selecting_words;
std::unique_ptr<std::string> m_suffix;