Skip to content

armgit / WacOS

Public Imported from GitHub
0

A new style desktop shell for Windows 11: menu bar, Dock, Finder, Spotlight, Control Center and Liquid Glass, in native C++.

Rust
5.2 KB
Raw Blame
//! A busca no computador, como no Finder: ao digitar, a janela vira uma visão de resultados (a pasta fica vazia) de
//! "Este Computador" ou da pasta atual com subpastas. Uma thread pergunta ao índice e manda os caminhos em lotes.
use std::collections::HashSet;
use std::ffi::c_void;
use std::sync::atomic::{AtomicI32, Ordering};

use comum::windows::Win32::Foundation::*;
use comum::windows::Win32::System::SystemInformation::GetTickCount;
use comum::windows::Win32::UI::Shell::SHChangeNotifyDeregister;
use comum::windows::Win32::UI::WindowsAndMessaging::PostMessageW;

use crate::base::*;
use crate::estado::*;
use crate::pasta::*;
use crate::{search, thumbs};

pub struct SearchBatch {
    pub paths: Vec<String>,
    pub done: bool,
}
const SEARCH_MAX: i32 = 3000;
pub static SEARCH_GEN: AtomicI32 = AtomicI32::new(0);
pub fn bump_search_gen() -> i32 {
    SEARCH_GEN.fetch_add(1, Ordering::SeqCst) + 1
}
fn post(h: isize, gen: i32, b: SearchBatch) {
    let raw = Box::into_raw(Box::new(b));
    unsafe {
        if PostMessageW(HWND(h as *mut c_void), crate::WM_APP_SEARCH, WPARAM(gen as usize), LPARAM(raw as isize)).is_err() {
            drop(Box::from_raw(raw));
        }
    }
}
/// primeiro o índice do Spotlight (nomes, instantâneo); se não houver resultado na pasta, a varredura pelo nome
fn search_thread(h: isize, gen: i32, q: String, folder: String) {
    let mut b = SearchBatch { paths: Vec::new(), done: false };
    let mut last = unsafe { GetTickCount() };
    let mut seen: HashSet<String> = HashSet::new();
    let mut once = |p: &str| -> bool {
        if SEARCH_GEN.load(Ordering::SeqCst) != gen {
            return false;
        }
        if !seen.insert(p.to_lowercase()) {
            return true;
        }
        b.paths.push(p.into());
        let now = unsafe { GetTickCount() };
        if b.paths.len() >= 200 || now.wrapping_sub(last) > 150 {
            post(h, gen, std::mem::replace(&mut b, SearchBatch { paths: Vec::new(), done: false }));
            last = now;
        }
        true
    };
    let mut n = search::spotlight(&q, &folder, SEARCH_MAX, &mut once);
    if SEARCH_GEN.load(Ordering::SeqCst) == gen {
        n = n.max(0) + search::index(&q, &folder, SEARCH_MAX, &mut once).max(0);
    }
    if n <= 0 && !folder.is_empty() && SEARCH_GEN.load(Ordering::SeqCst) == gen {
        search::walk(&q, &folder, SEARCH_MAX, &mut once);
    }
    b.done = true;
    post(h, gen, b);
}
pub fn run_search() {
    crate::kill_timer(crate::T_SEARCH);
    if !s().searching || s().search.is_empty() {
        return;
    }
    let gen = bump_search_gen();
    s().gen += 1;
    thumbs::set_generation(s().gen);
    release_images();
    s().all.clear();
    s().items.clear();
    apply_filter();
    set_scroll(0.0);
    s().search_busy = true;
    let folder = if s().search_scope == 1 { s().search_home.clone() } else { String::new() };
    let (h, q) = (s().hwnd.0 as isize, s().search.string());
    if crate::modo_foto() {
        // a foto busca na hora, sem a thread
        search_thread_sync(gen, &q, &folder);
    } else {
        std::thread::spawn(move || search_thread(h, gen, q, folder));
    }
    crate::invalidate();
}
fn search_thread_sync(gen: i32, q: &str, folder: &str) {
    let mut v = Vec::new();
    let n = search::spotlight(q, folder, 400, &mut |p: &str| {
        v.push(p.to_string());
        true
    });
    if n <= 0 && !folder.is_empty() {
        search::walk(q, folder, 400, &mut |p: &str| {
            v.push(p.to_string());
            true
        });
    }
    on_search_batch(gen, SearchBatch { paths: v, done: true });
}
pub fn enter_search() {
    if s().searching {
        return;
    }
    if s().rename_idx >= 0 {
        crate::vistas::end_rename(true);
    }
    s().search_home = s().folder.clone();
    s().searching = true;
    s().folder.clear();
    s().col_trail.clear();
    s().child_path.clear();
    unsafe {
        if s().notify_id != 0 {
            let _ = SHChangeNotifyDeregister(s().notify_id);
            s().notify_id = 0;
        }
    }
}
pub fn exit_search() {
    if !s().searching {
        return;
    }
    crate::kill_timer(crate::T_SEARCH);
    let home = s().search_home.clone();
    navigate(&home, false, "", false); // também limpa o campo e cancela a thread
}
/// Chamado a cada mudança no campo de busca.
pub fn on_search_changed() {
    if s().tag_view >= 0 || (!s().searching && s().folder.is_empty()) {
        apply_filter(); // etiqueta: filtro local
        return;
    }
    if s().search.is_empty() {
        exit_search();
        return;
    }
    enter_search();
    crate::set_title();
    if crate::modo_foto() {
        run_search();
    } else {
        crate::set_timer(crate::T_SEARCH, 250); // espera parar de digitar
    }
}
pub fn on_search_batch(gen: i32, b: SearchBatch) {
    if gen == SEARCH_GEN.load(Ordering::SeqCst) && s().searching {
        for p in &b.paths {
            if let Some(e) = entry_from_path(p) {
                s().all.push(e);
            }
        }
        if b.done {
            s().search_busy = false;
        }
        let sc = s().scroll;
        apply_filter();
        let m = sc.min(max_scroll());
        s().scroll = m;
        s().scroll_target = m;
        crate::invalidate();
    }
}