Text Editor: Use active text selection as search query #115247

Merged
Harley Acheson merged 4 commits from guishe/blender:quick-search into main 2023-11-30 18:27:02 +01:00
3 changed files with 35 additions and 19 deletions

View File

@ -376,19 +376,7 @@ static void text_properties_region_init(wmWindowManager *wm, ARegion *region)
static void text_properties_region_draw(const bContext *C, ARegion *region)
{
SpaceText *st = CTX_wm_space_text(C);
ED_region_panels(C, region);
/* this flag trick is make sure buttons have been added already */
if (st->flags & ST_FIND_ACTIVATE) {
if (UI_textbutton_activate_rna(C, region, st, "find_text")) {
/* if the panel was already open we need to do another redraw */
ScrArea *area = CTX_wm_area(C);
WM_event_add_notifier(C, NC_SPACE | ND_SPACE_TEXT, area);
}
st->flags &= ~ST_FIND_ACTIVATE;
}
}
static void text_id_remap(ScrArea * /*area*/, SpaceLink *slink, const IDRemapper *mappings)

View File

@ -15,6 +15,8 @@
#include "BKE_context.hh"
#include "BKE_screen.hh"
#include "DNA_text_types.h"
#include "ED_screen.hh"
#include "WM_types.hh"
@ -67,15 +69,42 @@ static int text_text_search_exec(bContext *C, wmOperator * /*op*/)
SpaceText *st = CTX_wm_space_text(C);
if (region) {
if (region->flag & RGN_FLAG_HIDDEN) {
ED_region_toggle_hidden(C, region);
ARegion *active_region = CTX_wm_region(C);
Text *text = st->text;
/* Use active text selection as search query, if selection is on a single line. */
if (active_region->regiontype == RGN_TYPE_WINDOW && text && text->curl == text->sell &&
text->curc != text->selc)
{
const char *sel_start = text->curl->line +
(text->curc < text->selc ? text->curc : text->selc);
const int sel_len = std::abs(text->curc - text->selc) + 1;
const int max_copy = sel_len < ST_MAX_FIND_STR ? sel_len : ST_MAX_FIND_STR;
BLI_strncpy(st->findstr, sel_start, max_copy);
}
UI_panel_category_active_set(region, "Text");
bool draw = false;
/* cannot send a button activate yet for case when region wasn't visible yet */
/* flag gets checked and cleared in main draw callback */
st->flags |= ST_FIND_ACTIVATE;
if (region->flag & RGN_FLAG_HIDDEN) {
ED_region_toggle_hidden(C, region);
draw = true;
}
const char *active_category = UI_panel_category_active_get(region, false);
if (active_category && !STREQ(active_category, "Text")) {
UI_panel_category_active_set(region, "Text");
draw = true;
}
/* Build the layout and draw so `find_text` text button can be activated. */
if (draw) {
ED_region_do_layout(C, region);
ED_region_do_draw(C, region);
}
UI_textbutton_activate_rna(C, region, st, "find_text");
ED_region_tag_redraw(region);
}

View File

@ -1474,7 +1474,6 @@ typedef enum eSpaceText_Flags {
ST_SHOW_MARGIN = (1 << 7),
ST_MATCH_CASE = (1 << 8),
ST_FIND_ACTIVATE = (1 << 9),
} eSpaceText_Flags;
/* SpaceText.findstr/replacestr */