Make ui::Entry smaller

This commit is contained in:
David Capello 2022-04-18 18:06:33 -03:00
parent 52667855d5
commit 83e2604d12
2 changed files with 57 additions and 26 deletions

View File

@ -1,5 +1,5 @@
// Aseprite UI Library // 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 // Copyright (C) 2001-2018 David Capello
// //
// This file is released under the terms of the MIT license. // This file is released under the terms of the MIT license.
@ -23,18 +23,22 @@
#include "ui/size_hint_event.h" #include "ui/size_hint_event.h"
#include "ui/system.h" #include "ui/system.h"
#include "ui/theme.h" #include "ui/theme.h"
#include "ui/timer.h"
#include "ui/widget.h" #include "ui/widget.h"
#include <algorithm> #include <algorithm>
#include <cctype> #include <cctype>
#include <cstdarg> #include <cstdarg>
#include <cstdio> #include <cstdio>
#include <memory>
namespace ui { namespace ui {
// Shared timer between all entries.
static std::unique_ptr<Timer> s_timer;
Entry::Entry(const int maxsize, const char* format, ...) Entry::Entry(const int maxsize, const char* format, ...)
: Widget(kEntryWidget) : Widget(kEntryWidget)
, m_timer(500, this)
, m_maxsize(maxsize) , m_maxsize(maxsize)
, m_caret(0) , m_caret(0)
, m_scroll(0) , m_scroll(0)
@ -71,6 +75,7 @@ Entry::Entry(const int maxsize, const char* format, ...)
Entry::~Entry() Entry::~Entry()
{ {
stopTimer();
} }
void Entry::setMaxTextLength(const int maxsize) void Entry::setMaxTextLength(const int maxsize)
@ -92,14 +97,14 @@ void Entry::showCaret()
{ {
m_hidden = false; m_hidden = false;
if (shouldStartTimer(hasFocus())) if (shouldStartTimer(hasFocus()))
m_timer.start(); startTimer();
invalidate(); invalidate();
} }
void Entry::hideCaret() void Entry::hideCaret()
{ {
m_hidden = true; m_hidden = true;
m_timer.stop(); stopTimer();
invalidate(); invalidate();
} }
@ -135,7 +140,7 @@ void Entry::setCaretPos(int pos)
} }
if (shouldStartTimer(hasFocus())) if (shouldStartTimer(hasFocus()))
m_timer.start(); startTimer();
m_state = true; m_state = true;
invalidate(); invalidate();
@ -195,10 +200,18 @@ Entry::Range Entry::selectedRange() const
void Entry::setSuffix(const std::string& suffix) void Entry::setSuffix(const std::string& suffix)
{ {
if (m_suffix != suffix) { // No-op cases
m_suffix = suffix; if ((!m_suffix && suffix.empty()) ||
invalidate(); (m_suffix && *m_suffix == suffix))
} return;
m_suffix = std::make_unique<std::string>(suffix);
invalidate();
}
std::string Entry::getSuffix()
{
return (m_suffix ? *m_suffix: std::string());
} }
void Entry::setTranslateDeadKeys(bool state) void Entry::setTranslateDeadKeys(bool state)
@ -224,16 +237,16 @@ bool Entry::onProcessMessage(Message* msg)
switch (msg->type()) { switch (msg->type()) {
case kTimerMessage: case kTimerMessage:
if (hasFocus() && static_cast<TimerMessage*>(msg)->timer() == &m_timer) { if (hasFocus() && static_cast<TimerMessage*>(msg)->timer() == s_timer.get()) {
// Blinking caret // Blinking caret
m_state = m_state ? false: true; m_state = (m_state ? false: true);
invalidate(); invalidate();
} }
break; break;
case kFocusEnterMessage: case kFocusEnterMessage:
if (shouldStartTimer(true)) if (shouldStartTimer(true))
m_timer.start(); startTimer();
m_state = true; m_state = true;
invalidate(); invalidate();
@ -254,7 +267,7 @@ bool Entry::onProcessMessage(Message* msg)
case kFocusLeaveMessage: case kFocusLeaveMessage:
invalidate(); invalidate();
m_timer.stop(); stopTimer();
if (!m_lock_selection) if (!m_lock_selection)
deselectText(); deselectText();
@ -399,7 +412,7 @@ bool Entry::onProcessMessage(Message* msg)
// Show the caret // Show the caret
if (is_dirty) { if (is_dirty) {
if (shouldStartTimer(true)) if (shouldStartTimer(true))
m_timer.start(); startTimer();
m_state = true; m_state = true;
} }
@ -889,4 +902,20 @@ void Entry::deleteRange(const Range& range, std::string& text)
m_caret = range.from; m_caret = range.from;
} }
void Entry::startTimer()
{
if (s_timer)
s_timer->stop();
s_timer = std::make_unique<Timer>(500, this);
s_timer->start();
}
void Entry::stopTimer()
{
if (s_timer) {
s_timer->stop();
s_timer.reset();
}
}
} // namespace ui } // namespace ui

View File

@ -1,5 +1,5 @@
// Aseprite UI Library // 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 // Copyright (C) 2001-2017 David Capello
// //
// This file is released under the terms of the MIT license. // This file is released under the terms of the MIT license.
@ -10,9 +10,10 @@
#pragma once #pragma once
#include "obs/signal.h" #include "obs/signal.h"
#include "ui/timer.h"
#include "ui/widget.h" #include "ui/widget.h"
#include <memory>
namespace ui { namespace ui {
class MouseMessage; class MouseMessage;
@ -49,7 +50,7 @@ namespace ui {
Range selectedRange() const; Range selectedRange() const;
void setSuffix(const std::string& suffix); void setSuffix(const std::string& suffix);
const std::string& getSuffix() { return m_suffix; } std::string getSuffix();
void setTranslateDeadKeys(bool state); void setTranslateDeadKeys(bool state);
@ -103,6 +104,8 @@ namespace ui {
void recalcCharBoxes(const std::string& text); void recalcCharBoxes(const std::string& text);
bool shouldStartTimer(const bool hasFocus); bool shouldStartTimer(const bool hasFocus);
void deleteRange(const Range& range, std::string& text); void deleteRange(const Range& range, std::string& text);
void startTimer();
void stopTimer();
class CalcBoxesTextDelegate; class CalcBoxesTextDelegate;
@ -113,21 +116,20 @@ namespace ui {
CharBox() { codepoint = from = to = width = 0; } CharBox() { codepoint = from = to = width = 0; }
}; };
typedef std::vector<CharBox> CharBoxes; using CharBoxes = std::vector<CharBox>;
CharBoxes m_boxes; CharBoxes m_boxes;
Timer m_timer;
int m_maxsize; int m_maxsize;
int m_caret; int m_caret;
int m_scroll; int m_scroll;
int m_select; int m_select;
bool m_hidden; bool m_hidden : 1;
bool m_state; // show or not the text caret bool m_state : 1; // show or not the text caret
bool m_readonly; bool m_readonly : 1;
bool m_recent_focused; bool m_recent_focused : 1;
bool m_lock_selection; bool m_lock_selection : 1;
bool m_translate_dead_keys; bool m_translate_dead_keys : 1;
std::string m_suffix; std::unique_ptr<std::string> m_suffix;
}; };
} // namespace ui } // namespace ui