Add search field on Font Popup

Added support to set child visibility property to ListBox items.
This commit is contained in:
David Capello 2017-03-15 14:24:42 -03:00
parent 76485d1e8a
commit b6cf0f218c
8 changed files with 167 additions and 45 deletions

View File

@ -118,7 +118,6 @@ frames = Frames:
pixel_ratio = Apply pixel ratio
[font_popup]
available_fonts = Available Fonts:
load = Load
[frame_properties]

View File

@ -1,8 +1,8 @@
<!-- Aseprite -->
<!-- Copyright (C) 2015-2016 by David Capello -->
<!-- Copyright (C) 2015-2017 by David Capello -->
<gui>
<vbox id="font_popup">
<label text="@.available_fonts" />
<search id="search" magnet="true" />
<view id="view" expansive="true" />
<hbox>
<boxfiller />

View File

@ -13,6 +13,7 @@
#include "app/commands/command.h"
#include "app/context.h"
#include "app/file_selector.h"
#include "app/match_words.h"
#include "app/resource_finder.h"
#include "app/tools/tool.h"
#include "app/tools/tool_box.h"
@ -487,8 +488,7 @@ private:
void fillSearchList(const std::string& search) {
deleteList(searchList());
std::vector<std::string> parts;
base::split_string(base::string_to_lower(search), parts, " ");
MatchWords match(search);
ListBox* listBoxes[] = { menus(), commands(), tools(), actions() };
int sectionIdx = 0; // index 0 is menus
@ -498,31 +498,24 @@ private:
for (auto item : listBox->children()) {
if (KeyItem* keyItem = dynamic_cast<KeyItem*>(item)) {
std::string itemText = keyItem->searchableText();
std::string lowerItemText = base::string_to_lower(itemText);
int matches = 0;
if (!match(itemText))
continue;
for (const auto& part : parts) {
if (lowerItemText.find(part) != std::string::npos)
++matches;
if (!group) {
group = new Separator(
section()->children()[sectionIdx]->text(), HORIZONTAL);
group->setStyle(SkinTheme::instance()->styles.separatorInView());
searchList()->addChild(group);
}
if (matches == int(parts.size())) {
if (!group) {
group = new Separator(
section()->children()[sectionIdx]->text(), HORIZONTAL);
group->setStyle(SkinTheme::instance()->styles.separatorInView());
KeyItem* copyItem =
new KeyItem(itemText,
keyItem->key(),
keyItem->menuitem(), 0);
searchList()->addChild(group);
}
KeyItem* copyItem =
new KeyItem(itemText,
keyItem->key(),
keyItem->menuitem(), 0);
m_allKeyItems.push_back(copyItem);
searchList()->addChild(copyItem);
}
m_allKeyItems.push_back(copyItem);
searchList()->addChild(copyItem);
}
}

44
src/app/match_words.h Normal file
View File

@ -0,0 +1,44 @@
// Aseprite
// Copyright (C) 2017 David Capello
//
// This program is distributed under the terms of
// the End-User License Agreement for Aseprite.
#ifndef APP_MATCH_WORDS_H_INCLUDED
#define APP_MATCH_WORDS_H_INCLUDED
#pragma once
#include "base/split_string.h"
#include "base/string.h"
#include <string>
#include <vector>
namespace app {
class MatchWords {
public:
MatchWords(const std::string& search) {
base::split_string(base::string_to_lower(search),
m_parts, " ");
}
bool operator()(const std::string& item) {
std::string lowerItem = base::string_to_lower(item);
std::size_t matches = 0;
for (const auto& part : m_parts) {
if (lowerItem.find(part) != std::string::npos)
++matches;
}
return (matches == m_parts.size());
}
private:
std::vector<std::string> m_parts;
};
} // namespace app
#endif

View File

@ -14,6 +14,8 @@
#include "app/commands/commands.h"
#include "app/console.h"
#include "app/font_path.h"
#include "app/match_words.h"
#include "app/ui/search_entry.h"
#include "app/ui/skin/skin_theme.h"
#include "app/ui_context.h"
#include "app/util/freetype_utils.h"
@ -137,6 +139,7 @@ FontPopup::FontPopup()
addChild(m_popup);
m_popup->search()->Change.connect(base::Bind<void>(&FontPopup::onSearchChange, this));
m_popup->loadFont()->Click.connect(base::Bind<void>(&FontPopup::onLoadFont, this));
m_listBox.setFocusMagnet(true);
m_listBox.Change.connect(base::Bind<void>(&FontPopup::onChangeFont, this));
@ -190,6 +193,23 @@ void FontPopup::showPopup(const gfx::Rect& bounds)
openWindow();
}
void FontPopup::onSearchChange()
{
std::string searchText = m_popup->search()->text();
Widget* firstItem = nullptr;
MatchWords match(searchText);
for (auto child : m_listBox.children()) {
bool visible = match(child->text());
if (visible && !firstItem)
firstItem = child;
child->setVisible(visible);
}
m_listBox.selectChild(firstItem);
layout();
}
void FontPopup::onChangeFont()
{
m_popup->loadFont()->setEnabled(true);

View File

@ -1,5 +1,5 @@
// Aseprite
// Copyright (C) 2001-2016 David Capello
// Copyright (C) 2001-2017 David Capello
//
// This program is distributed under the terms of
// the End-User License Agreement for Aseprite.
@ -31,6 +31,7 @@ namespace app {
obs::signal<void(const std::string&)> Load;
protected:
void onSearchChange();
void onChangeFont();
void onLoadFont();

View File

@ -1,5 +1,5 @@
// Aseprite UI Library
// Copyright (C) 2001-2016 David Capello
// Copyright (C) 2001-2017 David Capello
//
// This file is released under the terms of the MIT license.
// Read LICENSE.txt for more information.
@ -270,33 +270,42 @@ bool ListBox::onProcessMessage(Message* msg)
case kKeyDownMessage:
if (hasFocus() && !children().empty()) {
int select = getSelectedIndex();
View* view = View::getView(this);
int bottom = MAX(0, children().size()-1);
View* view = View::getView(this);
KeyMessage* keymsg = static_cast<KeyMessage*>(msg);
KeyScancode scancode = keymsg->scancode();
switch (keymsg->scancode()) {
if (keymsg->onlyCmdPressed()) {
if (scancode == kKeyUp) scancode = kKeyHome;
if (scancode == kKeyDown) scancode = kKeyEnd;
}
switch (scancode) {
case kKeyUp:
// Select previous element.
if (select >= 0)
select--;
if (select >= 0) {
select = advanceIndexThroughVisibleItems(select, -1, true);
}
// Or select the bottom of the list if there is no
// selected item.
else
select = bottom;
else {
select = advanceIndexThroughVisibleItems(bottom+1, -1, true);
}
break;
case kKeyDown:
select++;
select = advanceIndexThroughVisibleItems(select, +1, true);
break;
case kKeyHome:
select = 0;
select = advanceIndexThroughVisibleItems(-1, +1, false);
break;
case kKeyEnd:
select = bottom;
select = advanceIndexThroughVisibleItems(bottom+1, -1, false);
break;
case kKeyPageUp:
if (view) {
gfx::Rect vp = view->viewportBounds();
select -= vp.h / textHeight();
select = advanceIndexThroughVisibleItems(
select, -vp.h / textHeight(), false);
}
else
select = 0;
@ -304,10 +313,12 @@ bool ListBox::onProcessMessage(Message* msg)
case kKeyPageDown:
if (view) {
gfx::Rect vp = view->viewportBounds();
select += vp.h / textHeight();
select = advanceIndexThroughVisibleItems(
select, +vp.h / textHeight(), false);
}
else
else {
select = bottom;
}
break;
case kKeyLeft:
case kKeyRight:
@ -350,23 +361,33 @@ void ListBox::onResize(ResizeEvent& ev)
Rect cpos = childrenBounds();
for (auto child : children()) {
if (child->hasFlags(HIDDEN))
continue;
cpos.h = child->sizeHint().h;
child->setBounds(cpos);
cpos.y += child->bounds().h + this->childSpacing();
cpos.y += child->bounds().h + childSpacing();
}
}
void ListBox::onSizeHint(SizeHintEvent& ev)
{
int w = 0, h = 0;
int visibles = 0;
UI_FOREACH_WIDGET_WITH_END(children(), it, end) {
Size reqSize = (*it)->sizeHint();
for (auto child : children()) {
if (child->hasFlags(HIDDEN))
continue;
Size reqSize = child->sizeHint();
w = MAX(w, reqSize.w);
h += reqSize.h + (it+1 != end ? this->childSpacing(): 0);
h += reqSize.h;
++visibles;
}
if (visibles > 1)
h += childSpacing() * (visibles-1);
w += border().width();
h += border().height();
@ -384,4 +405,45 @@ void ListBox::onDoubleClickItem()
DoubleClickItem();
}
int ListBox::advanceIndexThroughVisibleItems(
int startIndex, int delta, const bool loop)
{
const int bottom = MAX(0, children().size()-1);
const int sgn = SGN(delta);
int index = startIndex;
startIndex = MID(0, startIndex, bottom);
int lastVisibleIndex = startIndex;
bool cycle = false;
while (delta) {
index += sgn;
if (cycle && index == startIndex) {
break;
}
else if (index < 0) {
if (!loop)
break;
index = bottom-sgn;
cycle = true;
}
else if (index > bottom) {
if (!loop)
break;
index = 0-sgn;
cycle = true;
}
else {
Widget* item = getChildByIndex(index);
if (item && !item->hasFlags(HIDDEN)) {
lastVisibleIndex = index;
delta -= sgn;
}
}
}
return lastVisibleIndex;
}
} // namespace ui

View File

@ -1,5 +1,5 @@
// Aseprite UI Library
// Copyright (C) 2001-2016 David Capello
// Copyright (C) 2001-2017 David Capello
//
// This file is released under the terms of the MIT license.
// Read LICENSE.txt for more information.
@ -50,6 +50,9 @@ namespace ui {
int getChildIndex(Widget* item);
Widget* getChildByIndex(int index);
int advanceIndexThroughVisibleItems(
int startIndex, int delta, const bool loop);
// True if this listbox accepts selecting multiple items at the
// same time.
bool m_multiselect;