From 83e2604d1265655268e40ccc6bb3a59878725a31 Mon Sep 17 00:00:00 2001 From: David Capello Date: Mon, 18 Apr 2022 18:06:33 -0300 Subject: [PATCH] Make ui::Entry smaller --- src/ui/entry.cpp | 57 ++++++++++++++++++++++++++++++++++++------------ src/ui/entry.h | 26 ++++++++++++---------- 2 files changed, 57 insertions(+), 26 deletions(-) diff --git a/src/ui/entry.cpp b/src/ui/entry.cpp index cab714a69..9b1cb7fc5 100644 --- a/src/ui/entry.cpp +++ b/src/ui/entry.cpp @@ -1,5 +1,5 @@ // Aseprite UI Library -// Copyright (C) 2018-2020 Igara Studio S.A. +// Copyright (C) 2018-2022 Igara Studio S.A. // Copyright (C) 2001-2018 David Capello // // This file is released under the terms of the MIT license. @@ -23,18 +23,22 @@ #include "ui/size_hint_event.h" #include "ui/system.h" #include "ui/theme.h" +#include "ui/timer.h" #include "ui/widget.h" #include #include #include #include +#include namespace ui { +// Shared timer between all entries. +static std::unique_ptr s_timer; + Entry::Entry(const int maxsize, const char* format, ...) : Widget(kEntryWidget) - , m_timer(500, this) , m_maxsize(maxsize) , m_caret(0) , m_scroll(0) @@ -71,6 +75,7 @@ Entry::Entry(const int maxsize, const char* format, ...) Entry::~Entry() { + stopTimer(); } void Entry::setMaxTextLength(const int maxsize) @@ -92,14 +97,14 @@ void Entry::showCaret() { m_hidden = false; if (shouldStartTimer(hasFocus())) - m_timer.start(); + startTimer(); invalidate(); } void Entry::hideCaret() { m_hidden = true; - m_timer.stop(); + stopTimer(); invalidate(); } @@ -135,7 +140,7 @@ void Entry::setCaretPos(int pos) } if (shouldStartTimer(hasFocus())) - m_timer.start(); + startTimer(); m_state = true; invalidate(); @@ -195,10 +200,18 @@ Entry::Range Entry::selectedRange() const void Entry::setSuffix(const std::string& suffix) { - if (m_suffix != suffix) { - m_suffix = suffix; - invalidate(); - } + // No-op cases + if ((!m_suffix && suffix.empty()) || + (m_suffix && *m_suffix == suffix)) + return; + + m_suffix = std::make_unique(suffix); + invalidate(); +} + +std::string Entry::getSuffix() +{ + return (m_suffix ? *m_suffix: std::string()); } void Entry::setTranslateDeadKeys(bool state) @@ -224,16 +237,16 @@ bool Entry::onProcessMessage(Message* msg) switch (msg->type()) { case kTimerMessage: - if (hasFocus() && static_cast(msg)->timer() == &m_timer) { + if (hasFocus() && static_cast(msg)->timer() == s_timer.get()) { // Blinking caret - m_state = m_state ? false: true; + m_state = (m_state ? false: true); invalidate(); } break; case kFocusEnterMessage: if (shouldStartTimer(true)) - m_timer.start(); + startTimer(); m_state = true; invalidate(); @@ -254,7 +267,7 @@ bool Entry::onProcessMessage(Message* msg) case kFocusLeaveMessage: invalidate(); - m_timer.stop(); + stopTimer(); if (!m_lock_selection) deselectText(); @@ -399,7 +412,7 @@ bool Entry::onProcessMessage(Message* msg) // Show the caret if (is_dirty) { if (shouldStartTimer(true)) - m_timer.start(); + startTimer(); m_state = true; } @@ -889,4 +902,20 @@ void Entry::deleteRange(const Range& range, std::string& text) m_caret = range.from; } +void Entry::startTimer() +{ + if (s_timer) + s_timer->stop(); + s_timer = std::make_unique(500, this); + s_timer->start(); +} + +void Entry::stopTimer() +{ + if (s_timer) { + s_timer->stop(); + s_timer.reset(); + } +} + } // namespace ui diff --git a/src/ui/entry.h b/src/ui/entry.h index c23ce3801..19d52f308 100644 --- a/src/ui/entry.h +++ b/src/ui/entry.h @@ -1,5 +1,5 @@ // Aseprite UI Library -// Copyright (C) 2018-2020 Igara Studio S.A. +// Copyright (C) 2018-2022 Igara Studio S.A. // Copyright (C) 2001-2017 David Capello // // This file is released under the terms of the MIT license. @@ -10,9 +10,10 @@ #pragma once #include "obs/signal.h" -#include "ui/timer.h" #include "ui/widget.h" +#include + namespace ui { class MouseMessage; @@ -49,7 +50,7 @@ namespace ui { Range selectedRange() const; void setSuffix(const std::string& suffix); - const std::string& getSuffix() { return m_suffix; } + std::string getSuffix(); void setTranslateDeadKeys(bool state); @@ -103,6 +104,8 @@ namespace ui { void recalcCharBoxes(const std::string& text); bool shouldStartTimer(const bool hasFocus); void deleteRange(const Range& range, std::string& text); + void startTimer(); + void stopTimer(); class CalcBoxesTextDelegate; @@ -113,21 +116,20 @@ namespace ui { CharBox() { codepoint = from = to = width = 0; } }; - typedef std::vector CharBoxes; + using CharBoxes = std::vector; CharBoxes m_boxes; - Timer m_timer; int m_maxsize; int m_caret; int m_scroll; int m_select; - bool m_hidden; - bool m_state; // show or not the text caret - bool m_readonly; - bool m_recent_focused; - bool m_lock_selection; - bool m_translate_dead_keys; - std::string m_suffix; + bool m_hidden : 1; + bool m_state : 1; // show or not the text caret + bool m_readonly : 1; + bool m_recent_focused : 1; + bool m_lock_selection : 1; + bool m_translate_dead_keys : 1; + std::unique_ptr m_suffix; }; } // namespace ui