Skip to content

Commit

Permalink
when system tray and start hidden on system tray are enabled, load do…
Browse files Browse the repository at this point in the history
…cument only after the user click the systray icon to show cherrytree (#1751)
  • Loading branch information
giuspen committed Jul 18, 2021
1 parent a678264 commit 28b1f16
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 6 deletions.
32 changes: 27 additions & 5 deletions src/ct/ct_app.cc
Original file line number Diff line number Diff line change
Expand Up @@ -130,12 +130,17 @@ void CtApp::on_activate()
Glib::RefPtr<Gio::File> r_file = Gio::File::create_for_path(CtApp::_uCtCfg->recentDocsFilepaths.front().string());
if (r_file->query_exists()) {
const std::string canonicalPath = fs::canonical(r_file->get_path()).string();
if (not pAppWindow->file_open(canonicalPath, "")) {
spdlog::warn("Couldn't open file: %s", canonicalPath);
if (not pAppWindow->start_on_systray_is_active()) {
if (not pAppWindow->file_open(canonicalPath, "")) {
spdlog::warn("%s Couldn't open file: %s", __FUNCTION__, canonicalPath);
}
}
else {
pAppWindow->start_on_systray_delayed_file_open_set(canonicalPath, "");
}
}
else {
spdlog::info("Last doc not found: {}", CtApp::_uCtCfg->recentDocsFilepaths.front());
spdlog::info("%s Last doc not found: {}", __FUNCTION__, CtApp::_uCtCfg->recentDocsFilepaths.front());
CtApp::_uCtCfg->recentDocsFilepaths.move_or_push_back(CtApp::_uCtCfg->recentDocsFilepaths.front());
pAppWindow->menu_set_items_recent_documents();
}
Expand Down Expand Up @@ -215,8 +220,13 @@ void CtApp::on_open(const Gio::Application::type_vec_files& files, const Glib::u
// there is not a window already running with that document
pAppWindow = _create_window();
const std::string canonicalPath = fs::canonical(r_file->get_path()).string();
if (not pAppWindow->file_open(canonicalPath, _node_to_focus)) {
spdlog::warn("Couldn't open file: {}", canonicalPath);
if (not pAppWindow->start_on_systray_is_active()) {
if (not pAppWindow->file_open(canonicalPath, _node_to_focus)) {
spdlog::warn("%s Couldn't open file: {}", __FUNCTION__, canonicalPath);
}
}
else {
pAppWindow->start_on_systray_delayed_file_open_set(canonicalPath, _node_to_focus);
}
if (get_windows().size() == 1) {
// start of main instance
Expand Down Expand Up @@ -331,12 +341,23 @@ void CtApp::systray_show_hide_windows()
while (gtk_events_pending()) gtk_main_iteration();
bool to_show{true};
for (Gtk::Window* pWin : get_windows()) {
// if any window is visible, we will hide
#ifdef _WIN32
if (pWin->get_visible()) {
#else
if (pWin->has_toplevel_focus()) {
#endif
to_show = false;
break;
}
}
if (not to_show) {
// check if any window should not be hidden
for (Gtk::Window* pWin : get_windows()) {
if (not dynamic_cast<CtMainWin*>(pWin)->get_systray_can_hide()) {
to_show = true;
break;
}
}
}
for (Gtk::Window* pWin : get_windows()) {
Expand All @@ -345,6 +366,7 @@ void CtApp::systray_show_hide_windows()
win->present();
win->restore_position();
win->set_visible(true);
(void)win->start_on_systray_delayed_file_open_kick();
}
else {
win->save_position();
Expand Down
28 changes: 27 additions & 1 deletion src/ct/ct_main_win.cc
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ CtMainWin::CtMainWin(bool no_gui,
set_visible(false);
}
else {
if (_pCtConfig->systrayOn and _pCtConfig->startOnSystray) {
if (start_on_systray_is_active()) {
/* Calling the 'present()' function apparently sets up selected
node visibility within the TreeView panel, whereas this
node setup is skipped when only calling 'set_visible(false)'.
Expand Down Expand Up @@ -178,6 +178,32 @@ CtMainWin::~CtMainWin()
//std::cout << "~CtMainWin" << std::endl;
}

bool CtMainWin::start_on_systray_is_active() const
{
return _pCtConfig->systrayOn and _pCtConfig->startOnSystray;
}

void CtMainWin::start_on_systray_delayed_file_open_set(const std::string& filepath, const std::string& nodename)
{
_startOnSystray_delayedFilepath = filepath;
_startOnSystray_delayedNodeName = nodename;
}

bool CtMainWin::start_on_systray_delayed_file_open_kick()
{
if (not _startOnSystray_delayedFilepath.empty()) {
const std::string startOnSystray_delayedFilepath = _startOnSystray_delayedFilepath;
const std::string startOnSystray_delayedNodeName = _startOnSystray_delayedNodeName;
_startOnSystray_delayedFilepath.clear();
_startOnSystray_delayedNodeName.clear();
if (file_open(startOnSystray_delayedFilepath, startOnSystray_delayedNodeName)) {
return true;
}
spdlog::warn("%s Couldn't open file: %s", __FUNCTION__, _startOnSystray_delayedFilepath);
}
return false;
}

std::string CtMainWin::get_code_icon_name(std::string code_type)
{
for (const auto& iconPair : CtConst::NODE_CODE_ICONS) {
Expand Down
9 changes: 9 additions & 0 deletions src/ct/ct_main_win.h
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,12 @@ class CtMainWin : public Gtk::ApplicationWindow
void save_position() { get_position(_savedXpos, _savedYpos); }
void restore_position() { if (_savedXpos != -1) move(_savedXpos, _savedYpos); }

bool start_on_systray_is_active() const;
void start_on_systray_delayed_file_open_set(const std::string& filepath, const std::string& nodename);
bool start_on_systray_delayed_file_open_kick();
void set_systray_can_hide(const bool systrayCanHide) { _systrayCanHide = systrayCanHide; }
bool get_systray_can_hide() const { return _systrayCanHide; }

private:
bool _on_window_key_press_event(GdkEventKey* event);
bool _on_window_configure_event(GdkEventConfigure* configure_event);
Expand Down Expand Up @@ -283,6 +289,9 @@ class CtMainWin : public Gtk::ApplicationWindow
bool _tree_just_auto_expanded{false};
std::unordered_map<gint64, int> _nodesCursorPos;
std::unordered_map<gint64, int> _nodesVScrollPos;
std::string _startOnSystray_delayedFilepath;
std::string _startOnSystray_delayedNodeName;
bool _systrayCanHide{true};

public:
sigc::signal<void> signal_app_new_instance = sigc::signal<void>();
Expand Down
4 changes: 4 additions & 0 deletions src/ct/ct_storage_control.cc
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,10 @@ Glib::RefPtr<Gsv::Buffer> CtStorageControl::get_delayed_text_buffer(const gint64
{
if (password.empty()) {
CtDialogTextEntry dialogTextEntry(title, true/*forPassword*/, pCtMainWin);
auto on_scope_exit = scope_guard([pCtMainWin](void*) {
pCtMainWin->set_systray_can_hide(true);
});
pCtMainWin->set_systray_can_hide(false);
if (Gtk::RESPONSE_OK != dialogTextEntry.run()) {
// no password, user cancels operation, return empty path
return fs::path{};
Expand Down

0 comments on commit 28b1f16

Please sign in to comment.