Skip to content

Commit

Permalink
feat: vue 页面改为同步加载;移除冗余的输出语句
Browse files Browse the repository at this point in the history
  • Loading branch information
GuoJikun committed Jan 2, 2025
1 parent 882b498 commit ea52c8d
Show file tree
Hide file tree
Showing 10 changed files with 89 additions and 114 deletions.
149 changes: 72 additions & 77 deletions src-tauri/src/preview.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ use windows::{
Foundation::{BOOL, HWND, LPARAM, LRESULT, WPARAM},
System::{
Com::{
CoCreateInstance, CoInitializeEx, CoUninitialize, IDispatch, IServiceProvider,
CLSCTX_SERVER, COINIT_APARTMENTTHREADED,
CoCreateInstance, CoInitializeEx, CoUninitialize, IDispatch, IServiceProvider, CLSCTX_LOCAL_SERVER, CLSCTX_SERVER, COINIT_APARTMENTTHREADED, COINIT_MULTITHREADED
},
SystemServices::SFGAO_FILESYSTEM,
Variant,
},
UI::{
Controls,
Input::KeyboardAndMouse,
Shell::{
IShellBrowser, IShellItemArray, IShellView, IShellWindows, ShellWindows, SIGDN_DESKTOPABSOLUTEPARSING, SIGDN_FILESYSPATH, SVGIO_SELECTION, SWFO_NEEDDISPATCH
Expand Down Expand Up @@ -52,6 +52,7 @@ impl Selected {
return match focused_type.as_str() {
"explorer" => unsafe { Self::get_select_file_from_explorer().ok() },
"desktop" => unsafe { Self::get_select_file_from_desktop().ok() },
#[cfg(debug_assertions)]
"dialog" => Self::get_select_file_from_dialog().ok(),
_ => None,
};
Expand Down Expand Up @@ -122,35 +123,7 @@ impl Selected {
}

let shell_view = shell_browser.QueryActiveShellView().unwrap();
let shell_items = shell_view.GetItemObject::<IShellItemArray>(SVGIO_SELECTION);

if shell_items.is_err() {
continue;
}
let shell_items = shell_items.unwrap();
let count = shell_items.GetCount().unwrap_or_default();
for i in 0..count {
let shell_item = shell_items.GetItemAt(i).unwrap();

// 如果不是文件对象则继续循环
if let Ok(attrs) = shell_item.GetAttributes(SFGAO_FILESYSTEM) {
log::info!("attrs: {:?}", attrs);
if attrs.0 == 0 {
continue;
}
}

if let Ok(display_name) = shell_item.GetDisplayName(SIGDN_FILESYSPATH) {
target_path = display_name.to_string().unwrap();
break;
}
if let Ok(display_name) =
shell_item.GetDisplayName(SIGDN_DESKTOPABSOLUTEPARSING)
{
target_path = display_name.to_string().unwrap();
break;
}
}
target_path = Self::get_selected_file_path_from_shellview(shell_view);
}

CoUninitialize();
Expand Down Expand Up @@ -204,23 +177,15 @@ impl Selected {
.unwrap();

let shell_view = shell_browser.QueryActiveShellView().unwrap();
let shell_items = shell_view
.GetItemObject::<IShellItemArray>(SVGIO_SELECTION)
.unwrap();

let count = shell_items.GetCount().unwrap_or_default();
for i in 0..count {
let shell_item = shell_items.GetItemAt(i).unwrap();
let display_name = shell_item.GetDisplayName(SIGDN_FILESYSPATH).unwrap();
target_path = display_name.to_string().unwrap();
break;
}
target_path = Self::get_selected_file_path_from_shellview(shell_view);
CoUninitialize();
tx.send(target_path).unwrap();
});
let target_path = rx.recv().unwrap();
Ok(target_path)
}


fn get_select_file_from_dialog() -> Result<String, WError> {
let (tx, rx) = mpsc::channel();

Expand All @@ -230,23 +195,48 @@ impl Selected {
let mut target_path = String::new();
let hwnd_gfw = WindowsAndMessaging::GetForegroundWindow();
println!("hwnd_gfw: {:?}", hwnd_gfw);
// dump_window_hierarchy(hwnd_gfw);

let _ = CoInitializeEx(None, COINIT_APARTMENTTHREADED);

let mut def_view: Option<HWND> = None;
let _ = WindowsAndMessaging::EnumChildWindows(hwnd_gfw, Some(Self::get_select_file_from_dialog_proc), LPARAM(&mut def_view as *mut _ as isize));

if def_view.is_none() {
tx.send(target_path).unwrap();
return;
}

let def_view = def_view.unwrap();
println!("def_view: {:?}", def_view);
// let list_view_hwnd = WindowsAndMessaging::FindWindowExW(def_view, None, w!("DirectUIHWND"), None);
// if list_view_hwnd.is_err() {
// tx.send(target_path).unwrap();
// return;
// }
// let list_view_hwnd = list_view_hwnd.unwrap();
// println!("list_view_hwnd: {:?}", list_view_hwnd);
// let list_view_classname = win::get_window_class_name(list_view_hwnd);
// if !list_view_classname.contains("DirectUIHWND") {
// tx.send(target_path).unwrap();
// return;
// }


let msg_id = WindowsAndMessaging::RegisterWindowMessageW(w!("ShellView_GetIShellBrowser"));
println!("msg_id: {:?}", msg_id);

let _ = CoInitializeEx(None, COINIT_APARTMENTTHREADED);
let mut view_dispatch: Option<IShellBrowser> = None;
let tmp_q = WindowsAndMessaging::SendMessageW(
def_view,
msg_id,
WPARAM(0),
LPARAM(&mut view_dispatch as *mut _ as isize)
);
println!("tmp_q: {:?}", tmp_q);
println!("view_dispatch: {:?}", view_dispatch);

let mut unknown: Option<IUnknown> = None;
let msg_id = WindowsAndMessaging::RegisterWindowMessageW(w!("ShellView"));
println!("msg_id: {:?}", msg_id);
let result = WindowsAndMessaging::SendMessageW(
def_view,
msg_id,
Expand All @@ -264,36 +254,7 @@ impl Selected {

let shell_view = unknown.cast::<IShellView>().ok();
if let Some(shell_view) = shell_view {
let shell_items = shell_view.GetItemObject::<IShellItemArray>(SVGIO_SELECTION);

if shell_items.is_err() {
return;
}
println!("shell_items: {:?}", shell_items);
let shell_items = shell_items.unwrap();
let count = shell_items.GetCount().unwrap_or_default();
for i in 0..count {
let shell_item = shell_items.GetItemAt(i).unwrap();

// 如果不是文件对象则继续循环
if let Ok(attrs) = shell_item.GetAttributes(SFGAO_FILESYSTEM) {
log::info!("attrs: {:?}", attrs);
if attrs.0 == 0 {
continue;
}
}

if let Ok(display_name) = shell_item.GetDisplayName(SIGDN_FILESYSPATH) {
target_path = display_name.to_string().unwrap();
break;
}
if let Ok(display_name) =
shell_item.GetDisplayName(SIGDN_DESKTOPABSOLUTEPARSING)
{
target_path = display_name.to_string().unwrap();
break;
}
}
target_path = Self::get_selected_file_path_from_shellview(shell_view);
println!("target_path: {:?}", target_path);
}

Expand All @@ -307,12 +268,46 @@ impl Selected {
unsafe extern "system" fn get_select_file_from_dialog_proc(hwnd: HWND, lparam: LPARAM) -> BOOL {
let list_view = lparam.0 as *mut Option<HWND>;
let class_name = win::get_window_class_name(hwnd);
if class_name.contains("SHELLDLL_DefView") {
if class_name.contains("DirectUIHWND") {
*list_view = Some(hwnd);
return BOOL(0);
}
BOOL(1)
}

unsafe fn get_selected_file_path_from_shellview(shell_view: IShellView) -> String {
let mut target_path = String::new();
let shell_items = shell_view.GetItemObject::<IShellItemArray>(SVGIO_SELECTION);

if shell_items.is_err() {
return target_path;
}
println!("shell_items: {:?}", shell_items);
let shell_items = shell_items.unwrap();
let count = shell_items.GetCount().unwrap_or_default();
for i in 0..count {
let shell_item = shell_items.GetItemAt(i).unwrap();

// 如果不是文件对象则继续循环
if let Ok(attrs) = shell_item.GetAttributes(SFGAO_FILESYSTEM) {
log::info!("attrs: {:?}", attrs);
if attrs.0 == 0 {
continue;
}
}

if let Ok(display_name) = shell_item.GetDisplayName(SIGDN_FILESYSPATH) {
target_path = display_name.to_string().unwrap();
break;
}
if let Ok(display_name) = shell_item.GetDisplayName(SIGDN_DESKTOPABSOLUTEPARSING)
{
target_path = display_name.to_string().unwrap();
break;
}
}
target_path
}
}

impl Drop for Selected {
Expand Down
6 changes: 3 additions & 3 deletions src/components/layout-preview.vue
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const props = defineProps<Props>()
<template>
<div class="preview">
<Header class="preview-header" :file="props.file" />
<div class="preview-body" v-loading="props.loading">
<div class="preview-body" v-loading="props.loading" element-loading-background="rgba(210, 210, 210, 0.5)">
<slot></slot>
</div>
<Footer class="preview-footer" :file="props.file" />
Expand All @@ -28,7 +28,7 @@ const props = defineProps<Props>()
overflow: hidden;
position: relative;
&-header {
position: absolute;
position: sticky;
left: 0;
top: 0;
width: 100%;
Expand All @@ -41,7 +41,7 @@ const props = defineProps<Props>()
font-size: 12px;
}
&-body {
padding: 28px 0 20px;
height: calc(100% - 48px);
display: flex;
justify-content: center;
align-items: center;
Expand Down
1 change: 0 additions & 1 deletion src/views/preview/archive.vue
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ onMounted(async () => {
const val = fileInfo.value.path as string
const txt: Array<ExtractedFile> = await invoke('archive', { path: val, mode: 'zip' })
const treeData = buildFileTree(txt)
console.log('path', treeData)
content.value = treeData.children as Array<FileNode> // 根节点是虚拟的,所以直接取子节点
})
Expand Down
3 changes: 0 additions & 3 deletions src/views/preview/book.vue
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,7 @@ const canvasRef = ref<HTMLCanvasElement>()
const pager = ref<number>(1)
let pdf: any = null
onMounted(async () => {
console.log('BookSupport', PDFJS)
PDFJS.GlobalWorkerOptions.workerSrc = '/pdf/pdf.worker.mjs'
// PDFJS.CMap.url = '/pdf/cmaps/' // 本地路径
// PDFJS.CMap.loaded = true // 如果你使用的是打包的 CMap 文件
pager.value = 1
fileInfo.value = route?.query as unknown as FileInfo
const path = convertFileSrc(fileInfo.value.path)
Expand Down
4 changes: 2 additions & 2 deletions src/views/preview/document.vue
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ onMounted(async () => {
fileInfo.value = route?.query as unknown as FileInfo
const val = fileInfo.value.path as string
const docs: Docs = await invoke('document', { path: val, mode: fileInfo.value.extension })
type.value = docs.Excel ? DocType.Excel : DocType.Docx
console.log('docs', type.value)
type.value = docs.Excel ? DocType.Excel : docs.Docx ? DocType.Docx : DocType.Pptx
switch (type.value) {
case DocType.Excel:
content.value = docs.Excel as Array<Sheet>
Expand Down
5 changes: 1 addition & 4 deletions src/views/preview/image.vue
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,14 @@ const init = (path: string) => {
}
onMounted(() => {
fileInfo.value = route.query as unknown as FileInfo
console.log(fileInfo.value)
init(convertFileSrc(fileInfo.value.path))
})
</script>

<template>
<LayoutPreview :file="fileInfo">
<div class="image-support">
<div class="image-support-inner" id="canvas">
<!-- <img :src="props.src" alt="image" crossorigin="anonymous">-->
</div>
<div class="image-support-inner" id="canvas"></div>
</div>
</LayoutPreview>
</template>
Expand Down
18 changes: 2 additions & 16 deletions src/views/preview/index.vue
Original file line number Diff line number Diff line change
@@ -1,24 +1,10 @@
<script setup lang="ts">
import { Loading } from '@element-plus/icons-vue'
</script>
<script setup lang="ts"></script>

<template>
<router-view v-slot="{ Component }">
<template v-if="Component">
<transition mode="out-in">
<suspense>
<!-- 主要内容 -->
<component :is="Component"></component>
<!-- 加载中状态 -->
<template #fallback>
<div class="preview-loading">
<div>
<Loading class="spin" />
<p>加载中...</p>
</div>
</div>
</template>
</suspense>
<component :is="Component"></component>
</transition>
</template>
</router-view>
Expand Down
12 changes: 9 additions & 3 deletions src/views/preview/md.vue
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,35 @@ import { readTextFile } from '@/utils'
import { createMd } from '@/utils/markdown/index'
import MdViewer from '@/components/md-viewer/index.vue'
import type MarkdownIt from 'markdown-it'
const route = useRoute()
const md = await createMd()
let md: MarkdownIt | null = null
defineOptions({
name: 'MdSupport',
})
const file = ref<FileInfo>()
const content = ref<string>()
const loading = ref<boolean>(true)
onMounted(async () => {
if (md === null) {
md = await createMd()
}
loading.value = true
file.value = route?.query as unknown as FileInfo
const path = file.value.path as string
console.log('path', path)
const txt = await readTextFile(path)
content.value = md.render(txt)
loading.value = false
})
</script>

<template>
<LayoutPreview :file="file">
<LayoutPreview :file="file" :loading="loading">
<div class="md-support">
<div class="md-support-inner" id="markdown-body">
<MdViewer :key="file?.path" :content="content" />
Expand Down
3 changes: 0 additions & 3 deletions src/views/preview/text.vue
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@ onMounted(async () => {
const path = fileInfo.value.path as string
const txt = await readTextFile(path)
console.log('txt', txt)
content.value = txt
})
</script>
Expand Down
2 changes: 0 additions & 2 deletions src/views/settings.vue
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import { app } from '@tauri-apps/api'
const getConfig = async () => {
const config = await readTextFile('config.json', { baseDir: BaseDirectory.Resource })
const data = JSON.parse(config)
console.log(data)
const target = [
{ name: 'Markdown', code: 'Md', data: data['preview.markdown.checked'] },
{ name: '图片', code: 'Image', data: data['preview.image.checked'] },
Expand All @@ -46,7 +45,6 @@ const config = ref<Array<Config>>()
const version = ref<string>('')
onMounted(async () => {
config.value = await getConfig()
console.log(config.value)
version.value = await app.getVersion()
})
</script>
Expand Down

0 comments on commit ea52c8d

Please sign in to comment.