2021-10-13 21:50:42 +08:00
|
|
|
// Aseprite
|
2022-08-25 21:14:09 +08:00
|
|
|
// Copyright (C) 2021-2022 Igara Studio S.A.
|
2021-10-13 21:50:42 +08:00
|
|
|
//
|
|
|
|
// This program is distributed under the terms of
|
|
|
|
// the End-User License Agreement for Aseprite.
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "app/ui/layout_selector.h"
|
|
|
|
|
|
|
|
#include "app/app.h"
|
2022-08-25 21:14:09 +08:00
|
|
|
#include "app/i18n/strings.h"
|
2022-08-26 22:02:06 +08:00
|
|
|
#include "app/match_words.h"
|
2022-08-25 21:14:09 +08:00
|
|
|
#include "app/ui/button_set.h"
|
|
|
|
#include "app/ui/configure_timeline_popup.h"
|
2021-10-13 21:50:42 +08:00
|
|
|
#include "app/ui/main_window.h"
|
|
|
|
#include "app/ui/separator_in_view.h"
|
|
|
|
#include "app/ui/skin/skin_theme.h"
|
2022-08-26 03:31:42 +08:00
|
|
|
#include "fmt/format.h"
|
2022-08-26 22:02:06 +08:00
|
|
|
#include "ui/entry.h"
|
2021-10-13 21:50:42 +08:00
|
|
|
#include "ui/listitem.h"
|
2022-08-25 21:14:09 +08:00
|
|
|
#include "ui/tooltips.h"
|
2021-10-13 21:50:42 +08:00
|
|
|
#include "ui/window.h"
|
|
|
|
|
2022-08-26 03:31:42 +08:00
|
|
|
#include "new_layout.xml.h"
|
|
|
|
|
2021-10-13 21:50:42 +08:00
|
|
|
#define ANI_TICKS 5
|
|
|
|
|
|
|
|
namespace app {
|
|
|
|
|
|
|
|
using namespace app::skin;
|
|
|
|
using namespace ui;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2022-08-25 21:14:09 +08:00
|
|
|
// TODO Similar ButtonSet to the one in timeline_conf.xml
|
|
|
|
class TimelineButtons : public ButtonSet {
|
|
|
|
public:
|
|
|
|
TimelineButtons() : ButtonSet(2)
|
|
|
|
{
|
|
|
|
addItem(Strings::timeline_conf_left())->processMnemonicFromText();
|
|
|
|
addItem(Strings::timeline_conf_right())->processMnemonicFromText();
|
|
|
|
addItem(Strings::timeline_conf_bottom(), 2)->processMnemonicFromText();
|
|
|
|
|
2022-08-25 21:25:41 +08:00
|
|
|
auto& timelinePosOption = Preferences::instance().general.timelinePosition;
|
|
|
|
|
|
|
|
setSelectedButtonFromTimelinePosition(timelinePosOption());
|
2022-08-26 03:31:42 +08:00
|
|
|
m_timelinePosConn = timelinePosOption.AfterChange.connect(
|
2022-08-25 21:25:41 +08:00
|
|
|
[this](gen::TimelinePosition position) { setSelectedButtonFromTimelinePosition(position); });
|
2022-08-25 21:14:09 +08:00
|
|
|
|
|
|
|
InitTheme.connect([this] {
|
|
|
|
auto theme = skin::SkinTheme::get(this);
|
|
|
|
setStyle(theme->styles.separatorInView());
|
|
|
|
});
|
|
|
|
initTheme();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2022-08-25 21:25:41 +08:00
|
|
|
void setSelectedButtonFromTimelinePosition(gen::TimelinePosition pos)
|
|
|
|
{
|
|
|
|
int selItem = 0;
|
|
|
|
switch (pos) {
|
|
|
|
case gen::TimelinePosition::LEFT: selItem = 0; break;
|
|
|
|
case gen::TimelinePosition::RIGHT: selItem = 1; break;
|
|
|
|
case gen::TimelinePosition::BOTTOM: selItem = 2; break;
|
|
|
|
}
|
|
|
|
setSelectedItem(selItem, false);
|
|
|
|
}
|
|
|
|
|
2022-08-25 21:14:09 +08:00
|
|
|
void onItemChange(Item* item) override
|
|
|
|
{
|
|
|
|
ButtonSet::onItemChange(item);
|
|
|
|
ConfigureTimelinePopup::onChangeTimelinePosition(selectedItem());
|
2022-08-25 21:22:21 +08:00
|
|
|
|
|
|
|
// Show the timeline
|
2022-08-25 21:25:41 +08:00
|
|
|
App::instance()->mainWindow()->setTimelineVisibility(true);
|
2022-08-25 21:14:09 +08:00
|
|
|
}
|
2022-08-26 03:31:42 +08:00
|
|
|
|
|
|
|
obs::scoped_connection m_timelinePosConn;
|
2022-08-25 21:14:09 +08:00
|
|
|
};
|
|
|
|
|
2022-08-26 22:02:06 +08:00
|
|
|
// TODO this combobox is similar to FileSelector::CustomFileNameEntry
|
|
|
|
// and GotoFrameCommand::TagsEntry
|
|
|
|
class LayoutsEntry : public ComboBox {
|
|
|
|
public:
|
|
|
|
LayoutsEntry(Layouts& layouts) : m_layouts(layouts)
|
|
|
|
{
|
|
|
|
setEditable(true);
|
|
|
|
getEntryWidget()->Change.connect(&LayoutsEntry::onEntryChange, this);
|
|
|
|
fill(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
void fill(bool all)
|
|
|
|
{
|
|
|
|
deleteAllItems();
|
|
|
|
|
|
|
|
MatchWords match(getEntryWidget()->text());
|
|
|
|
|
|
|
|
bool matchAny = false;
|
|
|
|
for (auto& layout : m_layouts) {
|
|
|
|
if (match(layout->name())) {
|
|
|
|
matchAny = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (auto& layout : m_layouts) {
|
|
|
|
if (all || !matchAny || match(layout->name()))
|
|
|
|
addItem(layout->name());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void onEntryChange()
|
|
|
|
{
|
|
|
|
closeListBox();
|
|
|
|
fill(false);
|
|
|
|
if (getItemCount() > 0)
|
|
|
|
openListBox();
|
|
|
|
}
|
|
|
|
|
|
|
|
Layouts& m_layouts;
|
|
|
|
};
|
|
|
|
|
2021-10-13 21:50:42 +08:00
|
|
|
}; // namespace
|
|
|
|
|
2022-08-26 03:31:42 +08:00
|
|
|
class LayoutSelector::LayoutItem : public ListItem {
|
|
|
|
public:
|
|
|
|
enum LayoutId {
|
|
|
|
DEFAULT,
|
|
|
|
DEFAULT_MIRROR,
|
|
|
|
SAVE_LAYOUT,
|
|
|
|
USER_DEFINED,
|
|
|
|
};
|
|
|
|
|
|
|
|
LayoutItem(LayoutSelector* selector,
|
|
|
|
const LayoutId id,
|
|
|
|
const std::string& text,
|
|
|
|
const LayoutPtr layout = nullptr)
|
|
|
|
: ListItem(text)
|
|
|
|
, m_id(id)
|
2022-08-26 22:02:06 +08:00
|
|
|
, m_selector(selector)
|
2022-08-26 03:31:42 +08:00
|
|
|
, m_layout(layout)
|
|
|
|
{
|
|
|
|
ASSERT((id != USER_DEFINED && layout == nullptr) || (id == USER_DEFINED && layout != nullptr));
|
|
|
|
}
|
|
|
|
|
2022-08-26 22:02:06 +08:00
|
|
|
Layout* layout() const { return m_layout.get(); }
|
|
|
|
|
|
|
|
void setLayout(const LayoutPtr& layout) { m_layout = layout; }
|
|
|
|
|
2022-08-26 03:31:42 +08:00
|
|
|
void selectImmediately()
|
|
|
|
{
|
|
|
|
MainWindow* win = App::instance()->mainWindow();
|
|
|
|
|
|
|
|
switch (m_id) {
|
|
|
|
case LayoutId::DEFAULT: win->setDefaultLayout(); break;
|
|
|
|
case LayoutId::DEFAULT_MIRROR: win->setDefaultMirrorLayout(); break;
|
|
|
|
case LayoutId::USER_DEFINED:
|
|
|
|
ASSERT(m_layout);
|
|
|
|
if (m_layout)
|
|
|
|
win->loadUserLayout(m_layout.get());
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
// Do nothing
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void selectAfterClose()
|
|
|
|
{
|
|
|
|
MainWindow* win = App::instance()->mainWindow();
|
|
|
|
|
|
|
|
switch (m_id) {
|
|
|
|
case LayoutId::SAVE_LAYOUT: {
|
2022-08-26 22:02:06 +08:00
|
|
|
// Select the "Layout" separator (it's like selecting nothing)
|
|
|
|
// TODO improve the ComboBox to select a real "nothing" (with
|
|
|
|
// a placeholder text)
|
|
|
|
m_selector->m_comboBox.setSelectedItemIndex(0);
|
2022-08-26 03:31:42 +08:00
|
|
|
|
2022-08-26 22:02:06 +08:00
|
|
|
gen::NewLayout window;
|
|
|
|
LayoutsEntry name(m_selector->m_layouts);
|
|
|
|
name.getEntryWidget()->setMaxTextLength(128);
|
|
|
|
name.setFocusMagnet(true);
|
|
|
|
name.setValue(fmt::format("{} ({})",
|
|
|
|
Strings::new_layout_default_name(),
|
|
|
|
m_selector->m_layouts.size() + 1));
|
|
|
|
|
|
|
|
window.namePlaceholder()->addChild(&name);
|
2022-08-26 03:31:42 +08:00
|
|
|
window.openWindowInForeground();
|
|
|
|
if (window.closer() == window.ok()) {
|
2022-08-26 22:02:06 +08:00
|
|
|
auto layout = Layout::MakeFromDock(name.getValue(), win->customizableDock());
|
2022-08-26 03:31:42 +08:00
|
|
|
|
2022-08-26 04:54:59 +08:00
|
|
|
m_selector->addLayout(layout);
|
2022-08-26 03:31:42 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
// Do nothing
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
LayoutId m_id;
|
|
|
|
LayoutSelector* m_selector;
|
|
|
|
LayoutPtr m_layout;
|
|
|
|
};
|
|
|
|
|
2021-10-13 21:50:42 +08:00
|
|
|
void LayoutSelector::LayoutComboBox::onChange()
|
|
|
|
{
|
2022-08-26 03:31:42 +08:00
|
|
|
ComboBox::onChange();
|
2021-10-13 21:50:42 +08:00
|
|
|
if (auto item = dynamic_cast<LayoutItem*>(getSelectedItem())) {
|
2022-08-26 03:31:42 +08:00
|
|
|
item->selectImmediately();
|
|
|
|
m_selected = item;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void LayoutSelector::LayoutComboBox::onCloseListBox()
|
|
|
|
{
|
|
|
|
ComboBox::onCloseListBox();
|
|
|
|
if (m_selected) {
|
|
|
|
m_selected->selectAfterClose();
|
|
|
|
m_selected = nullptr;
|
2021-10-13 21:50:42 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-25 21:14:09 +08:00
|
|
|
LayoutSelector::LayoutSelector(TooltipManager* tooltipManager)
|
|
|
|
: m_button(SkinTheme::instance()->parts.iconUserData())
|
2021-10-13 21:50:42 +08:00
|
|
|
{
|
|
|
|
m_button.Click.connect([this]() { switchSelector(); });
|
|
|
|
|
|
|
|
m_comboBox.setVisible(false);
|
|
|
|
|
|
|
|
addChild(&m_comboBox);
|
|
|
|
addChild(&m_button);
|
2022-08-25 21:14:09 +08:00
|
|
|
|
|
|
|
setupTooltips(tooltipManager);
|
|
|
|
|
|
|
|
InitTheme.connect([this] {
|
|
|
|
noBorderNoChildSpacing();
|
|
|
|
m_comboBox.noBorderNoChildSpacing();
|
|
|
|
m_button.noBorderNoChildSpacing();
|
|
|
|
});
|
|
|
|
initTheme();
|
2021-10-13 21:50:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
LayoutSelector::~LayoutSelector()
|
|
|
|
{
|
|
|
|
stopAnimation();
|
|
|
|
}
|
|
|
|
|
2022-08-26 03:31:42 +08:00
|
|
|
void LayoutSelector::addLayout(const LayoutPtr& layout)
|
|
|
|
{
|
2022-08-26 22:02:06 +08:00
|
|
|
bool added = m_layouts.addLayout(layout);
|
|
|
|
if (added) {
|
|
|
|
auto item = m_comboBox.addItem(
|
|
|
|
new LayoutItem(this, LayoutItem::USER_DEFINED, layout->name(), layout));
|
|
|
|
m_comboBox.setSelectedItemIndex(item);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
for (auto item : m_comboBox) {
|
|
|
|
if (auto layoutItem = dynamic_cast<LayoutItem*>(item)) {
|
|
|
|
if (layoutItem->layout() && layoutItem->layout()->name() == layout->name()) {
|
|
|
|
layoutItem->setLayout(layout);
|
|
|
|
m_comboBox.setSelectedItem(item);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-08-26 03:31:42 +08:00
|
|
|
}
|
|
|
|
|
2021-10-13 21:50:42 +08:00
|
|
|
void LayoutSelector::onAnimationFrame()
|
|
|
|
{
|
|
|
|
switch (animation()) {
|
|
|
|
case ANI_NONE: break;
|
|
|
|
case ANI_EXPANDING:
|
|
|
|
case ANI_COLLAPSING: {
|
|
|
|
const double t = animationTime();
|
2022-08-25 21:14:09 +08:00
|
|
|
m_comboBox.setSizeHint(gfx::Size(int(inbetween(m_startSize.w, m_endSize.w, t)),
|
|
|
|
int(inbetween(m_startSize.h, m_endSize.h, t))));
|
2021-10-13 21:50:42 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (auto win = window())
|
|
|
|
win->layout();
|
|
|
|
}
|
|
|
|
|
|
|
|
void LayoutSelector::onAnimationStop(int animation)
|
|
|
|
{
|
|
|
|
switch (animation) {
|
|
|
|
case ANI_EXPANDING: m_comboBox.setSizeHint(m_endSize); break;
|
|
|
|
case ANI_COLLAPSING:
|
|
|
|
m_comboBox.setVisible(false);
|
|
|
|
m_comboBox.setSizeHint(m_endSize);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (auto win = window())
|
|
|
|
win->layout();
|
|
|
|
}
|
|
|
|
|
|
|
|
void LayoutSelector::switchSelector()
|
|
|
|
{
|
|
|
|
bool expand;
|
|
|
|
if (!m_comboBox.isVisible()) {
|
|
|
|
expand = true;
|
|
|
|
|
|
|
|
// Create the combobox for first time
|
|
|
|
if (m_comboBox.getItemCount() == 0) {
|
2022-08-25 21:14:09 +08:00
|
|
|
m_comboBox.addItem(new SeparatorInView("Layout", HORIZONTAL));
|
2022-08-26 03:31:42 +08:00
|
|
|
m_comboBox.addItem(new LayoutItem(this, LayoutItem::DEFAULT, "Default"));
|
|
|
|
m_comboBox.addItem(new LayoutItem(this, LayoutItem::DEFAULT_MIRROR, "Default / Mirror"));
|
2022-08-25 21:14:09 +08:00
|
|
|
m_comboBox.addItem(new SeparatorInView("Timeline", HORIZONTAL));
|
|
|
|
m_comboBox.addItem(new TimelineButtons());
|
2022-08-26 03:31:42 +08:00
|
|
|
m_comboBox.addItem(new SeparatorInView("User Layouts", HORIZONTAL));
|
|
|
|
m_comboBox.addItem(new LayoutItem(this, LayoutItem::SAVE_LAYOUT, "Save..."));
|
|
|
|
for (const auto& layout : m_layouts) {
|
|
|
|
m_comboBox.addItem(new LayoutItem(this, LayoutItem::USER_DEFINED, layout->name(), layout));
|
|
|
|
}
|
2021-10-13 21:50:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
m_comboBox.setVisible(true);
|
|
|
|
m_comboBox.resetSizeHint();
|
|
|
|
m_startSize = gfx::Size(0, 0);
|
|
|
|
m_endSize = m_comboBox.sizeHint();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
expand = false;
|
|
|
|
m_startSize = m_comboBox.bounds().size();
|
|
|
|
m_endSize = gfx::Size(0, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
m_comboBox.setSizeHint(m_startSize);
|
|
|
|
startAnimation((expand ? ANI_EXPANDING : ANI_COLLAPSING), ANI_TICKS);
|
|
|
|
}
|
|
|
|
|
2022-08-25 21:14:09 +08:00
|
|
|
void LayoutSelector::setupTooltips(TooltipManager* tooltipManager)
|
|
|
|
{
|
|
|
|
tooltipManager->addTooltipFor(&m_button, Strings::main_window_layout(), TOP);
|
|
|
|
}
|
|
|
|
|
2021-10-13 21:50:42 +08:00
|
|
|
} // namespace app
|