Fix combobox size hint when using some user themes

From: https://community.aseprite.org/t/2276
This commit is contained in:
David Capello 2018-11-26 16:46:02 -03:00
parent 23059c6309
commit aea8e9cd83
3 changed files with 33 additions and 15 deletions

View File

@ -1,4 +1,5 @@
// Aseprite UI Library
// Copyright (C) 2018 Igara Studio S.A.
// Copyright (C) 2001-2017 David Capello
//
// This file is released under the terms of the MIT license.
@ -420,23 +421,17 @@ void ComboBox::onResize(ResizeEvent& ev)
void ComboBox::onSizeHint(SizeHintEvent& ev)
{
Size entrySize = m_entry->sizeHint();
Size reqSize = entrySize;
Size reqSize(0, 0);
// Get the text-length of every item
auto end = m_items.end();
for (auto it = m_items.begin(); it != end; ++it) {
int item_w =
2*guiscale()+
font()->textLength((*it)->text().c_str())+
16*guiscale();
reqSize.w = MAX(reqSize.w, item_w);
}
// Calculate the max required width depending on the text-length of
// each item.
for (const auto& item : m_items)
reqSize |= Entry::sizeHintWithText(m_entry, item->text());
Size buttonSize = m_button->sizeHint();
reqSize.w += buttonSize.w;
reqSize.h = MAX(reqSize.h, buttonSize.h);
ev.setSizeHint(reqSize);
}

View File

@ -1,4 +1,5 @@
// Aseprite UI Library
// Copyright (C) 2018 Igara Studio S.A.
// Copyright (C) 2001-2018 David Capello
//
// This file is released under the terms of the MIT license.
@ -24,6 +25,7 @@
#include "ui/theme.h"
#include "ui/widget.h"
#include <algorithm>
#include <cctype>
#include <cstdarg>
#include <cstdio>
@ -431,18 +433,35 @@ bool Entry::onProcessMessage(Message* msg)
return Widget::onProcessMessage(msg);
}
// static
gfx::Size Entry::sizeHintWithText(Entry* entry,
const std::string& text)
{
int w =
entry->font()->textLength(text) +
+ 2*entry->theme()->getEntryCaretSize(entry).w
+ entry->border().width();
w = std::min(w, ui::display_w()/2);
int h =
+ entry->font()->height()
+ entry->border().height();
return gfx::Size(w, h);
}
void Entry::onSizeHint(SizeHintEvent& ev)
{
int trailing = font()->textLength(getSuffix());
trailing = MAX(trailing, 2*theme()->getEntryCaretSize(this).w);
int w =
+ font()->textLength("w") * MIN(m_maxsize, 6)
font()->textLength("w") * std::min(m_maxsize, 6) +
+ trailing
+ 2*guiscale()
+ border().width();
w = MIN(w, ui::display_w()/2);
w = std::min(w, ui::display_w()/2);
int h =
+ font()->height()

View File

@ -1,4 +1,5 @@
// Aseprite UI Library
// Copyright (C) 2018 Igara Studio S.A.
// Copyright (C) 2001-2017 David Capello
//
// This file is released under the terms of the MIT license.
@ -49,6 +50,9 @@ namespace ui {
int* selbeg, int* selend) const;
gfx::Rect getEntryTextBounds() const;
static gfx::Size sizeHintWithText(Entry* entry,
const std::string& text);
// Signals
obs::signal<void()> Change;