Skip to content

Commit

Permalink
added the ability to change properties of UiActions
Browse files Browse the repository at this point in the history
  • Loading branch information
igorkorsukov committed Sep 20, 2024
1 parent 740034c commit 7347bfc
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/framework/shortcuts/view/shortcutsmodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ void ShortcutsModel::load()
beginResetModel();
m_shortcuts.clear();

for (const UiAction& action : uiactionsRegister()->getActions()) {
for (const UiAction& action : uiactionsRegister()->actionList()) {
Shortcut shortcut = shortcutsRegister()->shortcut(action.code);
if (!shortcut.isValid()) {
shortcut.action = action.code;
Expand Down
41 changes: 35 additions & 6 deletions src/framework/ui/internal/uiactionsregister.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ void UiActionsRegister::reg(const IUiActionsModulePtr& module)
updateChecked(newActionCodeList);
updateShortcuts(newActionCodeList);

module->actionsChanged().onReceive(this, [this](const UiActionList& actions) {
updateActions(actions);
});

module->actionEnabledChanged().onReceive(this, [this](const ActionCodeList& codes) {
updateEnabled(codes);
m_actionStateChanged.send(codes);
Expand Down Expand Up @@ -95,12 +99,7 @@ const UiActionsRegister::Info& UiActionsRegister::info(const ActionCode& code) c
return null;
}

const UiAction& UiActionsRegister::action(const ActionCode& code) const
{
return info(code).action;
}

const std::vector<UiAction> UiActionsRegister::getActions() const
std::vector<UiAction> UiActionsRegister::actionList() const
{
std::vector<UiAction> allActions;

Expand All @@ -111,6 +110,16 @@ const std::vector<UiAction> UiActionsRegister::getActions() const
return allActions;
}

const UiAction& UiActionsRegister::action(const ActionCode& code) const
{
return info(code).action;
}

async::Channel<UiActionList> UiActionsRegister::actionsChanged() const
{
return m_actionsChanged;
}

UiActionState UiActionsRegister::actionState(const ActionCode& code) const
{
const Info& inf = info(code);
Expand Down Expand Up @@ -146,6 +155,26 @@ void UiActionsRegister::updateShortcutsAll()
}
}

void UiActionsRegister::updateActions(const UiActionList& actions)
{
ActionCodeList codes;
for (const UiAction& act : actions) {
Info& inf = info(act.code);
IF_ASSERT_FAILED(inf.isValid()) {
continue;
}

inf.action = act;

codes.push_back(act.code);
}

m_actionsChanged.send(actions);

updateEnabled(codes);
updateChecked(codes);
}

void UiActionsRegister::doUpdateEnabled(Info& inf,
const IUiContextResolverPtr& ctxResolver,
const UiContext& currentCtx,
Expand Down
9 changes: 8 additions & 1 deletion src/framework/ui/internal/uiactionsregister.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,12 @@ class UiActionsRegister : public IUiActionsRegister, public Injectable, public a
void init();

void reg(const IUiActionsModulePtr& actions) override;

const std::vector<UiAction> actionList() const override;

const UiAction& action(const actions::ActionCode& code) const override;
const std::vector<UiAction> getActions() const override;
async::Channel<UiActionList> actionsChanged() const override;

UiActionState actionState(const actions::ActionCode& code) const override;
async::Channel<actions::ActionCodeList> actionStateChanged() const override;

Expand All @@ -69,6 +73,8 @@ class UiActionsRegister : public IUiActionsRegister, public Injectable, public a
void updateShortcuts(const actions::ActionCodeList& codes);
void updateShortcutsAll();

void updateActions(const UiActionList& actions);

void updateEnabled(const actions::ActionCodeList& codes);
void updateEnabledAll();
void doUpdateEnabled(Info& inf, const IUiContextResolverPtr& ctxResolver, const UiContext& currentCtx,
Expand All @@ -78,6 +84,7 @@ class UiActionsRegister : public IUiActionsRegister, public Injectable, public a
void updateChecked(const actions::ActionCodeList& codes);

std::unordered_map<actions::ActionCode, Info> m_actions;
async::Channel<UiActionList> m_actionsChanged;
async::Channel<actions::ActionCodeList> m_actionStateChanged;
};
}
Expand Down
8 changes: 8 additions & 0 deletions src/framework/ui/iuiactionsmodule.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,14 @@ class IUiActionsModule
virtual ~IUiActionsModule() = default;

virtual const UiActionList& actionsList() const = 0;
virtual async::Channel<UiActionList> actionsChanged() const
{
//! NOTE Usually actions don't change,
//! so let's add a default implementation here.
static async::Channel<UiActionList> ch;
return ch;
}

virtual bool actionEnabled(const UiAction& act) const = 0;
virtual async::Channel<muse::actions::ActionCodeList> actionEnabledChanged() const = 0;

Expand Down
11 changes: 7 additions & 4 deletions src/framework/ui/iuiactionsregister.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,13 @@ class IUiActionsRegister : MODULE_EXPORT_INTERFACE

virtual void reg(const IUiActionsModulePtr& actions) = 0;

virtual const UiAction& action(const muse::actions::ActionCode& code) const = 0;
virtual const std::vector<UiAction> getActions() const = 0;
virtual UiActionState actionState(const muse::actions::ActionCode& code) const = 0;
virtual async::Channel<muse::actions::ActionCodeList> actionStateChanged() const = 0;
virtual std::vector<UiAction> actionList() const = 0;

virtual const UiAction& action(const actions::ActionCode& code) const = 0;
virtual async::Channel<UiActionList> actionsChanged() const = 0;

virtual UiActionState actionState(const actions::ActionCode& code) const = 0;
virtual async::Channel<actions::ActionCodeList> actionStateChanged() const = 0;
};
}

Expand Down

0 comments on commit 7347bfc

Please sign in to comment.