Skip to content

Commit

Permalink
implemented reset of zoom with Ctrl+0 (#1835)
Browse files Browse the repository at this point in the history
  • Loading branch information
giuspen committed Jun 28, 2024
1 parent 700cc81 commit 1599cf2
Show file tree
Hide file tree
Showing 9 changed files with 166 additions and 60 deletions.
6 changes: 5 additions & 1 deletion src/ct/ct_codebox.cc
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,10 @@ bool CtCodebox::_on_key_press_event(GdkEventKey* event)
_ctTextview.zoom_text(false, get_syntax_highlighting());
return true;
}
if (GDK_KEY_0 == event->keyval or GDK_KEY_KP_0 == event->keyval) {
_ctTextview.zoom_text(std::nullopt, get_syntax_highlighting());
return true;
}
}
}
//std::cout << "keyval " << event->keyval << std::endl;
Expand All @@ -371,7 +375,7 @@ bool CtCodebox::_on_key_press_event(GdkEventKey* event)
_ctTextview.list_change_level(iter_insert, list_info, false);
return true;
}
else if (not backward) {
if (not backward) {
_ctTextview.list_change_level(iter_insert, list_info, true);
return true;
}
Expand Down
9 changes: 7 additions & 2 deletions src/ct/ct_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -194,11 +194,18 @@ class CtConfig

// [fonts]
Glib::ustring rtFont{CtConst::FONT_RT_DEFAULT};
int rtResetFontSize{0};
Glib::ustring ptFont{CtConst::FONT_PT_DEFAULT};
int ptResetFontSize{0};
Glib::ustring treeFont{CtConst::FONT_TREE_DEFAULT};
int treeResetFontSize{0};
Glib::ustring codeFont{CtConst::FONT_CODE_DEFAULT};
int codeResetFontSize{0};
Glib::ustring vteFont{CtConst::FONT_VTE_DEFAULT};
Glib::ustring fallbackFontFamily{"Sans"};
bool msDedicatedFont{false};
Glib::ustring monospaceFont{CtConst::FONT_MS_DEFAULT};
int msResetFontSize{0};

// [colors]
std::string ttDefFg{CtConst::TREE_TEXT_LIGHT_FG};
Expand All @@ -207,8 +214,6 @@ class CtConfig
std::string ttSelBg{CtConst::TREE_TEXT_SEL_BG};
std::string monospaceFg{CtConst::DEFAULT_MONOSPACE_FG};
std::string monospaceBg;
bool msDedicatedFont{false};
Glib::ustring monospaceFont{CtConst::FONT_MS_DEFAULT};
CtScalableTag scalableH1{CtConst::SCALABLE_H1_DEFAULT};
CtScalableTag scalableH2{CtConst::SCALABLE_H2_DEFAULT};
CtScalableTag scalableH3{CtConst::SCALABLE_H3_DEFAULT};
Expand Down
33 changes: 26 additions & 7 deletions src/ct/ct_main_win.cc
Original file line number Diff line number Diff line change
Expand Up @@ -688,14 +688,33 @@ void CtMainWin::config_switch_tree_side()
_pCtConfig->hpanedPos = _hPaned.property_position();
}

void CtMainWin::_zoom_tree(bool is_increase)
void CtMainWin::_zoom_tree(const std::optional<bool> is_increase)
{
Glib::RefPtr<Gtk::StyleContext> context = _uCtTreeview->get_style_context();
const Pango::FontDescription fontDesc = context->get_font(context->get_state());
int size = fontDesc.get_size() / Pango::SCALE + (is_increase ? 1 : -1);
if (size < 6) size = 6;
_pCtConfig->treeFont = CtFontUtil::get_font_str(CtFontUtil::get_font_family(_pCtConfig->treeFont), size);
signal_app_apply_for_each_window([](CtMainWin* win) { win->update_theme(); win->window_header_update(); });
Glib::RefPtr<Gtk::StyleContext> pContext = _uCtTreeview->get_style_context();
const Pango::FontDescription fontDesc = pContext->get_font(pContext->get_state());
const int size_pre = fontDesc.get_size() / Pango::SCALE;
int size_new = 0;
if (is_increase.has_value()) {
if (0 == _pCtConfig->treeResetFontSize) {
_pCtConfig->treeResetFontSize = size_pre;
spdlog::debug("{} set reset to {}", __FUNCTION__, size_pre);
}
size_new = size_pre + (is_increase.value() ? 1 : -1);
}
else {
// it's a reset
if (0 == _pCtConfig->treeResetFontSize || size_pre == _pCtConfig->treeResetFontSize) {
spdlog::debug("{} reset not necessary", __FUNCTION__);
}
else {
size_new = _pCtConfig->treeResetFontSize;
}
}
if (size_new > 0) {
if (size_new < 6) size_new = 6;
_pCtConfig->treeFont = CtFontUtil::get_font_str(CtFontUtil::get_font_family(_pCtConfig->treeFont), size_new);
signal_app_apply_for_each_window([](CtMainWin* win) { win->update_theme(); win->window_header_update(); });
}
}

void CtMainWin::reset()
Expand Down
2 changes: 1 addition & 1 deletion src/ct/ct_main_win.h
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ class CtMainWin : public Gtk::ApplicationWindow

void _reset_CtTreestore_CtTreeview();
void _ensure_curr_doc_in_recent_docs();
void _zoom_tree(bool is_increase);
void _zoom_tree(const std::optional<bool> is_increase);
bool _try_move_focus_to_anchored_widget_if_on_it();

private:
Expand Down
50 changes: 28 additions & 22 deletions src/ct/ct_main_win_events.cc
Original file line number Diff line number Diff line change
Expand Up @@ -199,33 +199,35 @@ bool CtMainWin::_on_treeview_key_press_event(GdkEventKey* event)
get_tree_view().set_cursor_safe(fist_sibling);
return true;
}
else if (GDK_KEY_Down == event->keyval) {
if (GDK_KEY_Down == event->keyval) {
auto last_sibling = reduce(curr_tree_iter(), [](Gtk::TreeIter iter) { return ++iter;});
if (last_sibling)
get_tree_view().set_cursor_safe(last_sibling);
return true;
}
else if (GDK_KEY_Left == event->keyval) {
if (GDK_KEY_Left == event->keyval) {
auto fist_parent = reduce(curr_tree_iter(), [](Gtk::TreeIter iter) { return iter->parent();});
if (fist_parent)
get_tree_view().set_cursor_safe(fist_parent);
return true;
}
else if (GDK_KEY_Right == event->keyval) {
if (GDK_KEY_Right == event->keyval) {
auto last_child = reduce(curr_tree_iter(), [](Gtk::TreeIter iter) { return iter->children().begin();});
if (last_child)
get_tree_view().set_cursor_safe(last_child);
return true;
}
else {
if (GDK_KEY_plus == event->keyval or GDK_KEY_KP_Add == event->keyval or GDK_KEY_equal == event->keyval) {
_zoom_tree(true);
return true;
}
else if (GDK_KEY_minus == event->keyval or GDK_KEY_KP_Subtract == event->keyval) {
_zoom_tree(false);
return true;
}
if (GDK_KEY_plus == event->keyval or GDK_KEY_KP_Add == event->keyval or GDK_KEY_equal == event->keyval) {
_zoom_tree(true);
return true;
}
if (GDK_KEY_minus == event->keyval or GDK_KEY_KP_Subtract == event->keyval) {
_zoom_tree(false);
return true;
}
if (GDK_KEY_0 == event->keyval or GDK_KEY_KP_0 == event->keyval) {
_zoom_tree(std::nullopt);
return true;
}
}
else {
Expand All @@ -236,27 +238,27 @@ bool CtMainWin::_on_treeview_key_press_event(GdkEventKey* event)
get_tree_view().set_cursor_safe(curr_tree_iter().parent());
return true;
}
else if (GDK_KEY_Right == event->keyval) {
if (GDK_KEY_Right == event->keyval) {
get_tree_view().expand_row(get_tree_store().get_path(curr_tree_iter()), false);
return true;
}
else if (GDK_KEY_Return == event->keyval or GDK_KEY_KP_Enter == event->keyval) {
auto path = get_tree_store().get_path(curr_tree_iter());
if (_uCtTreeview->row_expanded(path))
_uCtTreeview->collapse_row(path);
if (GDK_KEY_Return == event->keyval or GDK_KEY_KP_Enter == event->keyval) {
auto p = get_tree_store().get_path(curr_tree_iter());
if (_uCtTreeview->row_expanded(p))
_uCtTreeview->collapse_row(p);
else
_uCtTreeview->expand_row(path, false);
_uCtTreeview->expand_row(p, false);
return true;
}
else if (GDK_KEY_Menu == event->keyval) {
if (GDK_KEY_Menu == event->keyval) {
_uCtMenu->get_popup_menu(CtMenu::POPUP_MENU_TYPE::Node)->popup(0, event->time);
return true;
}
else if (GDK_KEY_Tab == event->keyval or GDK_KEY_ISO_Left_Tab == event->keyval) {
if (GDK_KEY_Tab == event->keyval or GDK_KEY_ISO_Left_Tab == event->keyval) {
_uCtActions->toggle_focus_tree_text();
return true;
}
else if (GDK_KEY_Delete == event->keyval) {
if (GDK_KEY_Delete == event->keyval) {
_uCtActions->node_delete();
return true;
}
Expand Down Expand Up @@ -498,10 +500,14 @@ bool CtMainWin::_on_textview_event(GdkEvent* event)
_ctTextview.zoom_text(true, curr_tree_iter().get_node_syntax_highlighting());
return true;
}
else if (GDK_KEY_minus == event->key.keyval or GDK_KEY_KP_Subtract == event->key.keyval) {
if (GDK_KEY_minus == event->key.keyval or GDK_KEY_KP_Subtract == event->key.keyval) {
_ctTextview.zoom_text(false, curr_tree_iter().get_node_syntax_highlighting());
return true;
}
if (GDK_KEY_0 == event->key.keyval or GDK_KEY_KP_0 == event->key.keyval) {
_ctTextview.zoom_text(std::nullopt, curr_tree_iter().get_node_syntax_highlighting());
return true;
}
}
}
return false;
Expand Down
2 changes: 1 addition & 1 deletion src/ct/ct_menu_actions.cc
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ void CtMenu::init_actions(CtActions* pActions)
_("Toggle h5 Property of the Selected Text"), sigc::mem_fun(*pActions, &CtActions::apply_tag_h5)});
_actions.push_back(CtMenuAction{fmt_cat, "fmt_h6", "ct_fmt-txt-h6", _("Toggle h_6 Property"), KB_CONTROL+"6",
_("Toggle h6 Property of the Selected Text"), sigc::mem_fun(*pActions, &CtActions::apply_tag_h6)});
_actions.push_back(CtMenuAction{fmt_cat, "fmt_small", "ct_fmt-txt-small", _("Toggle _Small Property"), KB_CONTROL+"0",
_actions.push_back(CtMenuAction{fmt_cat, "fmt_small", "ct_fmt-txt-small", _("Toggle _Small Property"), None,
_("Toggle Small Property of the Selected Text"), sigc::mem_fun(*pActions, &CtActions::apply_tag_small)});
_actions.push_back(CtMenuAction{fmt_cat, "fmt_superscript", "ct_fmt-txt-superscript", _("Toggle Su_perscript Property"), None,
_("Toggle Superscript Property of the Selected Text"), sigc::mem_fun(*pActions, &CtActions::apply_tag_superscript)});
Expand Down
5 changes: 5 additions & 0 deletions src/ct/ct_pref_dlg.cc
Original file line number Diff line number Diff line change
Expand Up @@ -274,11 +274,13 @@ Gtk::Widget* CtPrefDlg::build_tab_interface()

auto f_on_font_rt_set = [this, fontbutton_rt](){
_pConfig->rtFont = fontbutton_rt->get_font_name();
_pConfig->rtResetFontSize = 0;
apply_for_each_window([](CtMainWin* win) { win->update_theme(); });
};
fontbutton_rt->signal_font_set().connect(f_on_font_rt_set);
checkbutton_ms->signal_toggled().connect([this, checkbutton_ms, fontbutton_ms](){
_pConfig->msDedicatedFont = checkbutton_ms->get_active();
_pConfig->msResetFontSize = 0;
fontbutton_ms->set_sensitive(_pConfig->msDedicatedFont);
if (auto tag = _pCtMainWin->get_text_tag_table()->lookup(CtConst::TAG_ID_MONOSPACE)) {
tag->property_family() = _pConfig->msDedicatedFont ? "" : CtConst::TAG_PROP_VAL_MONOSPACE;
Expand All @@ -296,11 +298,13 @@ Gtk::Widget* CtPrefDlg::build_tab_interface()
fontbutton_ms->signal_font_set().connect(f_on_font_ms_set);
auto f_on_font_pt_set = [this, fontbutton_pt](){
_pConfig->ptFont = fontbutton_pt->get_font_name();
_pConfig->ptResetFontSize = 0;
apply_for_each_window([](CtMainWin* win) { win->update_theme(); });
};
fontbutton_pt->signal_font_set().connect(f_on_font_pt_set);
auto f_on_font_code_set = [this, fontbutton_code](){
_pConfig->codeFont = fontbutton_code->get_font_name();
_pConfig->codeResetFontSize = 0;
apply_for_each_window([](CtMainWin* win) { win->update_theme(); });
};
fontbutton_code->signal_font_set().connect(f_on_font_code_set);
Expand All @@ -313,6 +317,7 @@ Gtk::Widget* CtPrefDlg::build_tab_interface()
#endif // HAVE_VTE
auto f_on_font_tree_set = [this, fontbutton_tree](){
_pConfig->treeFont = fontbutton_tree->get_font_name();
_pConfig->treeResetFontSize = 0;
apply_for_each_window([](CtMainWin* win) { win->update_theme(); win->window_header_update(); });
};
fontbutton_tree->signal_font_set().connect(f_on_font_tree_set);
Expand Down
117 changes: 92 additions & 25 deletions src/ct/ct_text_view.cc
Original file line number Diff line number Diff line change
Expand Up @@ -672,37 +672,104 @@ void CtTextView::cursor_and_tooltips_reset()
}

// Increase or Decrease Text Font
void CtTextView::zoom_text(const bool is_increase, const std::string& syntaxHighlighting)
void CtTextView::zoom_text(const std::optional<bool> is_increase, const std::string& syntaxHighlighting)
{
Glib::RefPtr<Gtk::StyleContext> context = get_style_context();
const Pango::FontDescription fontDesc = context->get_font(context->get_state());
int size = fontDesc.get_size() / Pango::SCALE + (is_increase ? 1 : -1);
if (size < 6) size = 6;

if (syntaxHighlighting == CtConst::RICH_TEXT_ID or syntaxHighlighting == CtConst::TABLE_CELL_TEXT_ID) {
_pCtConfig->rtFont = CtFontUtil::get_font_str(CtFontUtil::get_font_family(_pCtConfig->rtFont), size);
spdlog::debug("rtFont {}", _pCtConfig->rtFont);
// also fix monospace font size
if (_pCtConfig->msDedicatedFont and not _pCtConfig->monospaceFont.empty()) {
const Pango::FontDescription monoFontDesc(_pCtConfig->monospaceFont);
int monoSize = monoFontDesc.get_size() / Pango::SCALE + (is_increase ? 1 : -1);
if (monoSize < 6) monoSize = 6;

_pCtConfig->monospaceFont = CtFontUtil::get_font_str(CtFontUtil::get_font_family(_pCtConfig->monospaceFont), size);
if (auto tag = get_buffer()->get_tag_table()->lookup(CtConst::TAG_ID_MONOSPACE)) {
tag->property_font() = _pCtConfig->monospaceFont;
Glib::RefPtr<Gtk::StyleContext> pContext = get_style_context();
const Pango::FontDescription fontDesc = pContext->get_font(pContext->get_state());
const int size_pre = fontDesc.get_size() / Pango::SCALE;
const bool is_rt = syntaxHighlighting == CtConst::RICH_TEXT_ID or syntaxHighlighting == CtConst::TABLE_CELL_TEXT_ID;
const bool is_rt_ms_dedic = is_rt and _pCtConfig->msDedicatedFont and not _pCtConfig->monospaceFont.empty();
const bool is_pt = syntaxHighlighting == CtConst::PLAIN_TEXT_ID;
int size_ms_pre;
if (is_rt_ms_dedic) {
const Pango::FontDescription monoFontDesc(_pCtConfig->monospaceFont);
size_ms_pre = monoFontDesc.get_size() / Pango::SCALE;
}
int size_new = 0;
int size_ms_new = 0;
if (is_increase.has_value()) {
if (is_rt) {
if (0 == _pCtConfig->rtResetFontSize) {
_pCtConfig->rtResetFontSize = size_pre;
spdlog::debug("{} rt set reset to {}", __FUNCTION__, size_pre);
}
if (is_rt_ms_dedic) {
if (0 == _pCtConfig->msResetFontSize) {
_pCtConfig->msResetFontSize = size_ms_pre;
spdlog::debug("{} ms set reset to {}", __FUNCTION__, size_ms_pre);
}
size_ms_new = size_ms_pre + (is_increase.value() ? 1 : -1);
}
}
}
else if (syntaxHighlighting == CtConst::PLAIN_TEXT_ID) {
_pCtConfig->ptFont = CtFontUtil::get_font_str(CtFontUtil::get_font_family(_pCtConfig->ptFont), size);
spdlog::debug("ptFont {}", _pCtConfig->ptFont);
else if (is_pt) {
if (0 == _pCtConfig->ptResetFontSize) {
_pCtConfig->ptResetFontSize = size_pre;
spdlog::debug("{} pt set reset to {}", __FUNCTION__, size_pre);
}
}
else {
if (0 == _pCtConfig->codeResetFontSize) {
_pCtConfig->codeResetFontSize = size_pre;
spdlog::debug("{} code set reset to {}", __FUNCTION__, size_pre);
}
}
size_new = size_pre + (is_increase.value() ? 1 : -1);
}
else {
_pCtConfig->codeFont = CtFontUtil::get_font_str(CtFontUtil::get_font_family(_pCtConfig->codeFont), size);
spdlog::debug("codeFont {}", _pCtConfig->codeFont);
// it's a reset
if (is_rt) {
if (0 == _pCtConfig->rtResetFontSize || size_pre == _pCtConfig->rtResetFontSize) {
spdlog::debug("{} rt reset not necessary", __FUNCTION__);
}
else {
size_new = _pCtConfig->rtResetFontSize;
}
if (is_rt_ms_dedic) {
if (0 == _pCtConfig->msResetFontSize || size_ms_pre == _pCtConfig->msResetFontSize) {
spdlog::debug("{} ms reset not necessary", __FUNCTION__);
}
else {
size_ms_new = _pCtConfig->msResetFontSize;
}
}
}
else if (is_pt) {
if (0 == _pCtConfig->ptResetFontSize || size_pre == _pCtConfig->ptResetFontSize) {
spdlog::debug("{} pt reset not necessary", __FUNCTION__);
}
else {
size_new = _pCtConfig->ptResetFontSize;
}
}
else {
if (0 == _pCtConfig->codeResetFontSize || size_pre == _pCtConfig->codeResetFontSize) {
spdlog::debug("{} code reset not necessary", __FUNCTION__);
}
else {
size_new = _pCtConfig->codeResetFontSize;
}
}
}
if (size_new > 0) {
if (size_new < 6) size_new = 6;
if (is_rt) {
_pCtConfig->rtFont = CtFontUtil::get_font_str(CtFontUtil::get_font_family(_pCtConfig->rtFont), size_new);
if (size_ms_new > 0) {
if (size_ms_new < 6) size_ms_new = 6;
_pCtConfig->monospaceFont = CtFontUtil::get_font_str(CtFontUtil::get_font_family(_pCtConfig->monospaceFont), size_ms_new);
if (auto tag = get_buffer()->get_tag_table()->lookup(CtConst::TAG_ID_MONOSPACE)) {
tag->property_font() = _pCtConfig->monospaceFont;
}
}
}
else if (is_pt) {
_pCtConfig->ptFont = CtFontUtil::get_font_str(CtFontUtil::get_font_family(_pCtConfig->ptFont), size_new);
}
else {
_pCtConfig->codeFont = CtFontUtil::get_font_str(CtFontUtil::get_font_family(_pCtConfig->codeFont), size_new);
}
_pCtMainWin->signal_app_apply_for_each_window([](CtMainWin* win) { win->update_theme(); });
}
_pCtMainWin->signal_app_apply_for_each_window([](CtMainWin* win) { win->update_theme(); });
}

void CtTextView::set_spell_check(bool allow_on)
Expand Down
2 changes: 1 addition & 1 deletion src/ct/ct_text_view.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class CtTextView : public Gsv::View

void cursor_and_tooltips_handler(int x, int y);
void cursor_and_tooltips_reset();
void zoom_text(const bool is_increase, const std::string& syntaxHighlighting);
void zoom_text(const std::optional<bool> is_increase, const std::string& syntaxHighlighting);
void set_spell_check(bool allow_on);
void synch_spell_check_change_from_gspell_right_click_menu();

Expand Down

0 comments on commit 1599cf2

Please sign in to comment.