优化程序

This commit is contained in:
hero920103 committed 2026-10-12 14:57:46 +08:00
1 parent 68e8eb91a7
commit 6df29c9254
27 files changed
+1974 -203

No files matched your search

Binary file not shown.
Binary file not shown.
+180 -1
View File
@@ -5,7 +5,7 @@ import shutil
# 数据库放到 Windows 常见的 APPDATA 目录(避免写在项目目录)
APP_NAME = "CleanDesktopOrganizer"
_SCHEMA_VERSION_KEY = "schema_version"
LATEST_SCHEMA_VERSION = 1
LATEST_SCHEMA_VERSION = 7
_PROJECT_DB_PATH = os.path.join(
os.path.dirname(os.path.dirname(__file__)), "data.db"
@@ -119,8 +119,68 @@ def _apply_migrations(conn: sqlite3.Connection):
if not _table_has_column(conn, "groups", "folder_path"):
conn.execute("ALTER TABLE groups ADD COLUMN folder_path TEXT DEFAULT ''")
def m2():
# 桌面文件框
conn.execute(
"""
CREATE TABLE IF NOT EXISTS file_boxes (
id INTEGER PRIMARY KEY AUTOINCREMENT,
title TEXT NOT NULL DEFAULT '文件框',
x INTEGER DEFAULT 100,
y INTEGER DEFAULT 100,
w INTEGER DEFAULT 240,
h INTEGER DEFAULT 320,
collapsed INTEGER DEFAULT 0,
position INTEGER DEFAULT 0,
opacity REAL DEFAULT 0.5
)
"""
)
conn.execute(
"""
CREATE TABLE IF NOT EXISTS file_box_items (
id INTEGER PRIMARY KEY AUTOINCREMENT,
box_id INTEGER NOT NULL,
name TEXT NOT NULL,
path TEXT NOT NULL,
position INTEGER DEFAULT 0,
FOREIGN KEY (box_id) REFERENCES file_boxes(id) ON DELETE CASCADE
)
"""
)
def m3():
# 给已存在的 file_boxes 表补 opacity 字段
if not _table_has_column(conn, "file_boxes", "opacity"):
conn.execute("ALTER TABLE file_boxes ADD COLUMN opacity REAL DEFAULT 0.5")
def m4():
# folder_path:不为空时 FileBox 变成文件夹浏览模式,实时显示该目录内容
if not _table_has_column(conn, "file_boxes", "folder_path"):
conn.execute("ALTER TABLE file_boxes ADD COLUMN folder_path TEXT DEFAULT ''")
def m5():
# view_mode:拖入模式下的视图方式 —— "icon"(默认)或 "list"
if not _table_has_column(conn, "file_boxes", "view_mode"):
conn.execute("ALTER TABLE file_boxes ADD COLUMN view_mode TEXT DEFAULT 'icon'")
def m6():
# 把所有已存在的 file_boxes 的 opacity 刷成 0.5(之前是 1.0)
conn.execute("UPDATE file_boxes SET opacity=0.5 WHERE opacity IS NULL OR opacity >= 0.99")
def m7():
# group_id:关联主程序分组,非 NULL 时 FileBox 进入"分组联动模式"
if not _table_has_column(conn, "file_boxes", "group_id"):
conn.execute("ALTER TABLE file_boxes ADD COLUMN group_id INTEGER DEFAULT NULL")
migrations = {
1: m1,
2: m2,
3: m3,
4: m4,
5: m5,
6: m6,
7: m7,
}
cur = _get_schema_version(conn)
@@ -320,3 +380,122 @@ def reset_groups_and_items(recreate_default_group: bool = True):
conn.execute("INSERT INTO groups (name, position) VALUES ('常用程序', 0)")
conn.commit()
conn.close()
# ── FileBoxes ──────────────────────────────────────────
def get_file_boxes():
conn = get_conn()
rows = conn.execute("SELECT * FROM file_boxes ORDER BY position").fetchall()
conn.close()
return [dict(r) for r in rows]
def add_file_box(title: str, x: int = 100, y: int = 100, w: int = 240, h: int = 320) -> int:
conn = get_conn()
row = conn.execute("SELECT COALESCE(MAX(position), -1) FROM file_boxes").fetchone()
pos = (row[0] if row else -1) + 1
cur = conn.execute(
"INSERT INTO file_boxes (title, x, y, w, h, position) VALUES (?,?,?,?,?,?)",
(title, x, y, w, h, pos),
)
conn.commit()
box_id = cur.lastrowid
conn.close()
return box_id
def update_file_box(box_id: int, **kwargs):
"""更新 file_boxes 记录,支持 title/x/y/w/h/collapsed/group_id/folder_path/view_mode/opacity。"""
if not kwargs:
return
allowed = ("title", "x", "y", "w", "h", "collapsed", "group_id", "folder_path", "view_mode", "opacity")
fields = [k for k in kwargs.keys() if k in allowed]
if not fields:
return
conn = get_conn()
sets = ", ".join(f"{f}=?" for f in fields)
conn.execute(
f"UPDATE file_boxes SET {sets} WHERE id=?",
[kwargs[f] for f in fields] + [box_id],
)
conn.commit()
conn.close()
def create_file_box_from_group(group_id: int) -> int | None:
"""把已有分组变成桌面文件框(group_id 关联)。返回新 file_boxes.id,失败返回 None。"""
conn = get_conn()
# 检查分组存在
group = conn.execute("SELECT id, name FROM groups WHERE id=?", (group_id,)).fetchone()
if not group:
conn.close(); return None
# 检查是否已有关联的 FileBox(避免重复)
existing = conn.execute("SELECT id FROM file_boxes WHERE group_id=?", (group_id,)).fetchone()
if existing:
conn.close(); return existing["id"]
pos_row = conn.execute("SELECT COALESCE(MAX(position), -1) FROM file_boxes").fetchone()
pos = (pos_row[0] if pos_row else -1) + 1
cur = conn.execute(
"INSERT INTO file_boxes (title, x, y, w, h, position, opacity, group_id) VALUES (?,?,?,?,?,?,?,?)",
(group["name"], 150, 150, 260, 340, pos, 0.5, group_id),
)
conn.commit()
box_id = cur.lastrowid
conn.close()
return box_id
def delete_file_box(box_id: int):
conn = get_conn()
conn.execute("DELETE FROM file_box_items WHERE box_id=?", (box_id,))
conn.execute("DELETE FROM file_boxes WHERE id=?", (box_id,))
conn.commit()
conn.close()
def get_file_box_items(box_id: int):
conn = get_conn()
rows = conn.execute(
"SELECT * FROM file_box_items WHERE box_id=? ORDER BY position", (box_id,)
).fetchall()
conn.close()
return [dict(r) for r in rows]
def add_file_box_item(box_id: int, name: str, path: str) -> int:
conn = get_conn()
row = conn.execute(
"SELECT COALESCE(MAX(position), -1) FROM file_box_items WHERE box_id=?", (box_id,)
).fetchone()
pos = (row[0] if row else -1) + 1
cur = conn.execute(
"INSERT INTO file_box_items (box_id, name, path, position) VALUES (?,?,?,?)",
(box_id, name, path, pos),
)
conn.commit()
item_id = cur.lastrowid
conn.close()
return item_id
def delete_file_box_item(item_id: int):
conn = get_conn()
conn.execute("DELETE FROM file_box_items WHERE id=?", (item_id,))
conn.commit()
conn.close()
def update_file_box_item(item_id: int, **kwargs):
if not kwargs:
return
fields = [k for k in kwargs.keys() if k in ("name", "path", "position")]
if not fields:
return
conn = get_conn()
sets = ", ".join(f"{f}=?" for f in fields)
conn.execute(
f"UPDATE file_box_items SET {sets} WHERE id=?",
[kwargs[f] for f in fields] + [item_id],
)
conn.commit()
conn.close()