notesmgr 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- notesmgr/__init__.py +5 -0
- notesmgr/__main__.py +10 -0
- notesmgr/actions.py +86 -0
- notesmgr/application.py +93 -0
- notesmgr/button_row.py +149 -0
- notesmgr/clipboard_linux.py +58 -0
- notesmgr/clipboard_macos.py +78 -0
- notesmgr/clipboard_tool.py +84 -0
- notesmgr/clipboard_windows.py +198 -0
- notesmgr/cmd_line.py +63 -0
- notesmgr/commands.py +307 -0
- notesmgr/config.py +152 -0
- notesmgr/config_defaults.py +41 -0
- notesmgr/config_editor.py +62 -0
- notesmgr/config_files.py +147 -0
- notesmgr/console.py +62 -0
- notesmgr/descriptions.py +27 -0
- notesmgr/dialogs.py +389 -0
- notesmgr/editor_command.py +138 -0
- notesmgr/errors.py +14 -0
- notesmgr/explorer_drag.py +222 -0
- notesmgr/explorer_drop.py +195 -0
- notesmgr/explorer_font.py +81 -0
- notesmgr/explorer_tree.py +148 -0
- notesmgr/failure_report.py +92 -0
- notesmgr/file_watch.py +115 -0
- notesmgr/folder_ops.py +173 -0
- notesmgr/main_window.py +474 -0
- notesmgr/markdown_render.py +78 -0
- notesmgr/menu_bar.py +114 -0
- notesmgr/note_blocks.py +511 -0
- notesmgr/note_file.py +203 -0
- notesmgr/note_fonts.py +189 -0
- notesmgr/note_image.py +75 -0
- notesmgr/note_ops.py +316 -0
- notesmgr/note_panel.py +227 -0
- notesmgr/note_table.py +145 -0
- notesmgr/note_tags.py +241 -0
- notesmgr/note_text.py +62 -0
- notesmgr/note_view.py +273 -0
- notesmgr/order_file.py +150 -0
- notesmgr/project.py +168 -0
- notesmgr/project_ops.py +302 -0
- notesmgr/py.typed +0 -0
- notesmgr/rich_clipboard.py +107 -0
- notesmgr/session.py +71 -0
- notesmgr/shortcuts.py +131 -0
- notesmgr/trash.py +31 -0
- notesmgr/version_info.py +78 -0
- notesmgr-0.1.0.dist-info/METADATA +299 -0
- notesmgr-0.1.0.dist-info/RECORD +55 -0
- notesmgr-0.1.0.dist-info/WHEEL +5 -0
- notesmgr-0.1.0.dist-info/entry_points.txt +2 -0
- notesmgr-0.1.0.dist-info/licenses/LICENSE.txt +22 -0
- notesmgr-0.1.0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,222 @@
|
|
|
1
|
+
#! /usr/local/bin/python3
|
|
2
|
+
"""Moving the notes and the folders of the tree by dragging them."""
|
|
3
|
+
|
|
4
|
+
# Copyright (c) 2026 Tom Björkholm
|
|
5
|
+
# MIT License
|
|
6
|
+
|
|
7
|
+
import tkinter
|
|
8
|
+
from pathlib import Path
|
|
9
|
+
from tkinter import ttk
|
|
10
|
+
from typing import Callable, NamedTuple, Optional
|
|
11
|
+
from notesmgr.explorer_drop import Drop
|
|
12
|
+
|
|
13
|
+
PRESS_EVENT = '<ButtonPress-1>'
|
|
14
|
+
"""Tk event of the mouse button going down, which may begin a drag."""
|
|
15
|
+
|
|
16
|
+
MOTION_EVENT = '<B1-Motion>'
|
|
17
|
+
"""Tk event of the pointer moving with the mouse button held down."""
|
|
18
|
+
|
|
19
|
+
RELEASE_EVENT = '<ButtonRelease-1>'
|
|
20
|
+
"""Tk event of the mouse button going up, dropping what is dragged."""
|
|
21
|
+
|
|
22
|
+
CANCEL_EVENT = '<Escape>'
|
|
23
|
+
"""Tk event of the key that gives up a drag that is going on."""
|
|
24
|
+
|
|
25
|
+
DRAG_START = 5
|
|
26
|
+
"""Pixels the pointer moves before a press is taken for a drag.
|
|
27
|
+
|
|
28
|
+
A press that moves no further than this is the user choosing an item
|
|
29
|
+
of the tree rather than taking hold of it, so nothing is marked and
|
|
30
|
+
nothing is moved when the button goes up again.
|
|
31
|
+
"""
|
|
32
|
+
|
|
33
|
+
MARK_TAG = 'drop_mark'
|
|
34
|
+
"""Name of the tag that marks the folder a drop would land in."""
|
|
35
|
+
|
|
36
|
+
MARK_COLOUR = '#3a7bd5'
|
|
37
|
+
"""Colour of the marks that say where a drop lands, a strong blue."""
|
|
38
|
+
|
|
39
|
+
MARK_TEXT_COLOUR = '#ffffff'
|
|
40
|
+
"""Colour of the name of a marked folder, which the mark is behind."""
|
|
41
|
+
|
|
42
|
+
LINE_HEIGHT = 2
|
|
43
|
+
"""How thick the line that says where a note lands is, in pixels."""
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
class Dragging(NamedTuple):
|
|
47
|
+
"""What is being dragged now, and where it would land.
|
|
48
|
+
|
|
49
|
+
Nothing is being dragged while the item is None, which is how a
|
|
50
|
+
drag that was given up and a drag that has not begun are told
|
|
51
|
+
from one that is going on.
|
|
52
|
+
"""
|
|
53
|
+
|
|
54
|
+
item: Optional[Path] = None
|
|
55
|
+
drop: Optional[Drop] = None
|
|
56
|
+
origin: int = 0
|
|
57
|
+
started: bool = False
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
def row_at(tree: ttk.Treeview, height: int) -> Optional[Path]:
|
|
61
|
+
"""Return the item of a tree at a height, None for no item there."""
|
|
62
|
+
item = tree.identify_row(height)
|
|
63
|
+
return Path(item) if item else None
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
def row_box(tree: ttk.Treeview,
|
|
67
|
+
item: Path) -> Optional[tuple[int, int, int, int]]:
|
|
68
|
+
"""Return where the row of an item is, None when it is nowhere.
|
|
69
|
+
|
|
70
|
+
A row that is scrolled out of sight has no place in the tree, and
|
|
71
|
+
neither has any row of a window that is not on the screen, which
|
|
72
|
+
is what the None is for.
|
|
73
|
+
"""
|
|
74
|
+
box = tree.bbox(str(item))
|
|
75
|
+
return None if not box else box
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
def lower_half(tree: ttk.Treeview, over: Optional[Path], height: int) \
|
|
79
|
+
-> bool:
|
|
80
|
+
"""Return whether a height is in the lower half of an item's row."""
|
|
81
|
+
box = None if over is None else row_box(tree, over)
|
|
82
|
+
return box is not None and height >= box[1] + box[3] / 2
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
class DropMark:
|
|
86
|
+
"""What the tree shows of where a drop would land.
|
|
87
|
+
|
|
88
|
+
A drop into a folder is shown by marking that folder, and a drop
|
|
89
|
+
among the notes of a folder by a line at the edge of a row, which
|
|
90
|
+
is where the note would go.
|
|
91
|
+
"""
|
|
92
|
+
|
|
93
|
+
def __init__(self, tree: ttk.Treeview) -> None:
|
|
94
|
+
"""Get ready to mark a tree, marking nothing yet."""
|
|
95
|
+
self.tree = tree
|
|
96
|
+
self.marked: Optional[str] = None
|
|
97
|
+
self.line = tkinter.Frame(tree, height=LINE_HEIGHT,
|
|
98
|
+
background=MARK_COLOUR)
|
|
99
|
+
tree.tag_configure(MARK_TAG, background=MARK_COLOUR,
|
|
100
|
+
foreground=MARK_TEXT_COLOUR)
|
|
101
|
+
|
|
102
|
+
def at_folder(self, folder: Path) -> None:
|
|
103
|
+
"""Mark the folder that what is dragged would be put into."""
|
|
104
|
+
self.clear()
|
|
105
|
+
self.marked = str(folder)
|
|
106
|
+
self.tree.item(self.marked, tags=(MARK_TAG,))
|
|
107
|
+
|
|
108
|
+
def at_edge(self, over: Path, lower: bool) -> None:
|
|
109
|
+
"""Draw the line at an edge of a row, which a note lands at."""
|
|
110
|
+
self.clear()
|
|
111
|
+
box = row_box(self.tree, over)
|
|
112
|
+
if box is None:
|
|
113
|
+
return
|
|
114
|
+
edge = box[1] + box[3] if lower else box[1]
|
|
115
|
+
self.line.place(x=0, y=edge - LINE_HEIGHT // 2, relwidth=1)
|
|
116
|
+
|
|
117
|
+
def clear(self) -> None:
|
|
118
|
+
"""Take away the marks that said where a drop would land."""
|
|
119
|
+
if self.marked is not None and self.tree.exists(self.marked):
|
|
120
|
+
self.tree.item(self.marked, tags=())
|
|
121
|
+
self.marked = None
|
|
122
|
+
self.line.place_forget()
|
|
123
|
+
|
|
124
|
+
|
|
125
|
+
class ExplorerDrag:
|
|
126
|
+
"""Lets what the tree shows be moved by dragging it.
|
|
127
|
+
|
|
128
|
+
The view supplies nothing but coordinates: which item the pointer
|
|
129
|
+
is over, and whether it is in the lower half of that item. Where
|
|
130
|
+
that lands the dragged item is answered elsewhere, and this shows
|
|
131
|
+
the answer and asks for it to be carried out when the button
|
|
132
|
+
goes up.
|
|
133
|
+
|
|
134
|
+
A drop that lands nowhere is no drop at all, so a release over an
|
|
135
|
+
item that nothing can be dropped on, and Escape while dragging,
|
|
136
|
+
leave the project exactly as it was.
|
|
137
|
+
"""
|
|
138
|
+
|
|
139
|
+
def __init__(self, tree: ttk.Treeview,
|
|
140
|
+
target: Callable[[Path, Optional[Path], bool],
|
|
141
|
+
Optional[Drop]],
|
|
142
|
+
dropped: Callable[[Path, Drop], None]) -> None:
|
|
143
|
+
"""Let the items of a tree be dragged from now on.
|
|
144
|
+
|
|
145
|
+
Args:
|
|
146
|
+
tree: The tree whose items are dragged.
|
|
147
|
+
target: Asked where a dragged item would land, given the
|
|
148
|
+
item, what the pointer is over, and whether it is in
|
|
149
|
+
the lower half of that item.
|
|
150
|
+
dropped: Told what was dragged and where it was dropped.
|
|
151
|
+
"""
|
|
152
|
+
self.tree = tree
|
|
153
|
+
self.target = target
|
|
154
|
+
self.dropped = dropped
|
|
155
|
+
self.mark = DropMark(tree)
|
|
156
|
+
self.state = Dragging()
|
|
157
|
+
tree.bind(PRESS_EVENT, self.press)
|
|
158
|
+
tree.bind(MOTION_EVENT, self.motion)
|
|
159
|
+
tree.bind(RELEASE_EVENT, self.release)
|
|
160
|
+
tree.bind(CANCEL_EVENT, self.cancelled)
|
|
161
|
+
|
|
162
|
+
def press(self, event: 'tkinter.Event[ttk.Treeview]') -> None:
|
|
163
|
+
"""Take hold of the item that the mouse button went down on."""
|
|
164
|
+
self.cancel()
|
|
165
|
+
self.state = Dragging(item=row_at(self.tree, event.y), origin=event.y)
|
|
166
|
+
|
|
167
|
+
def motion(self, event: 'tkinter.Event[ttk.Treeview]') -> None:
|
|
168
|
+
"""Show where what is being dragged would land now."""
|
|
169
|
+
item = self.state.item
|
|
170
|
+
if item is None or not self.dragging(event.y):
|
|
171
|
+
return
|
|
172
|
+
over = row_at(self.tree, event.y)
|
|
173
|
+
lower = lower_half(self.tree, over, event.y)
|
|
174
|
+
self.state = self.state._replace(drop=self.target(item, over, lower))
|
|
175
|
+
self.show(over, lower)
|
|
176
|
+
|
|
177
|
+
def release(self, _event: 'tkinter.Event[ttk.Treeview]') -> None:
|
|
178
|
+
"""Move what was dragged to where it was dropped."""
|
|
179
|
+
item, drop = self.state.item, self.state.drop
|
|
180
|
+
self.cancel()
|
|
181
|
+
if item is not None and drop is not None:
|
|
182
|
+
self.dropped(item, drop)
|
|
183
|
+
|
|
184
|
+
def cancelled(self, _event: 'tkinter.Event[ttk.Treeview]') -> None:
|
|
185
|
+
"""Give up the drag, leaving the project exactly as it was."""
|
|
186
|
+
self.cancel()
|
|
187
|
+
|
|
188
|
+
def cancel(self) -> None:
|
|
189
|
+
"""Forget what was dragged, and unmark where it would land."""
|
|
190
|
+
self.mark.clear()
|
|
191
|
+
self.state = Dragging()
|
|
192
|
+
|
|
193
|
+
def dragging(self, height: int) -> bool:
|
|
194
|
+
"""Return whether the press has become a drag by now.
|
|
195
|
+
|
|
196
|
+
Args:
|
|
197
|
+
height: Where the pointer is in the tree.
|
|
198
|
+
|
|
199
|
+
Returns:
|
|
200
|
+
Whether the pointer has moved far enough from where the
|
|
201
|
+
button went down, which it goes on having done for the
|
|
202
|
+
rest of the drag.
|
|
203
|
+
"""
|
|
204
|
+
if not self.state.started and \
|
|
205
|
+
abs(height - self.state.origin) >= DRAG_START:
|
|
206
|
+
self.state = self.state._replace(started=True)
|
|
207
|
+
return self.state.started
|
|
208
|
+
|
|
209
|
+
def show(self, over: Optional[Path], lower: bool) -> None:
|
|
210
|
+
"""Mark where what is dragged would land, or mark nothing.
|
|
211
|
+
|
|
212
|
+
Args:
|
|
213
|
+
over: The item of the tree that the pointer is over.
|
|
214
|
+
lower: Whether the pointer is in the lower half of it.
|
|
215
|
+
"""
|
|
216
|
+
drop = self.state.drop
|
|
217
|
+
if drop is None or over is None:
|
|
218
|
+
self.mark.clear()
|
|
219
|
+
elif drop.folder == over:
|
|
220
|
+
self.mark.at_folder(over)
|
|
221
|
+
else:
|
|
222
|
+
self.mark.at_edge(over, lower)
|
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
#! /usr/local/bin/python3
|
|
2
|
+
"""Where a note or a folder lands when it is dropped in the tree."""
|
|
3
|
+
|
|
4
|
+
# Copyright (c) 2026 Tom Björkholm
|
|
5
|
+
# MIT License
|
|
6
|
+
|
|
7
|
+
from pathlib import Path
|
|
8
|
+
from typing import NamedTuple, Optional
|
|
9
|
+
from notesmgr.folder_ops import move_folder
|
|
10
|
+
from notesmgr.note_ops import move_note
|
|
11
|
+
from notesmgr.project import Folder, Project, folder_at
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
class Drop(NamedTuple):
|
|
15
|
+
"""Where a dragged item lands: a folder, and a place in it.
|
|
16
|
+
|
|
17
|
+
A note lands at a place among the notes of the folder, counted
|
|
18
|
+
as the notes stand before it is moved. A folder lands in the
|
|
19
|
+
folder itself and at no place in it, because the folders of a
|
|
20
|
+
folder are shown in alphabetical order rather than in an order
|
|
21
|
+
of their own, which is what a place of None says.
|
|
22
|
+
|
|
23
|
+
The place is called so rather than an index, because a tuple
|
|
24
|
+
numbers what it holds through a method of that name already.
|
|
25
|
+
"""
|
|
26
|
+
|
|
27
|
+
folder: Path
|
|
28
|
+
place: Optional[int]
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
def folder_drop(project: Project, dragged: Path, over: Path) \
|
|
32
|
+
-> Optional[Drop]:
|
|
33
|
+
"""Return where a dragged folder lands, None when it cannot land.
|
|
34
|
+
|
|
35
|
+
A folder goes into a folder of the project and nowhere else. It
|
|
36
|
+
goes neither into itself nor into a folder of its own, which
|
|
37
|
+
would take it out of the project altogether, and the root folder
|
|
38
|
+
of the project is the project itself and does not move.
|
|
39
|
+
|
|
40
|
+
Args:
|
|
41
|
+
project: The project as the tree is showing it.
|
|
42
|
+
dragged: The folder that is being dragged.
|
|
43
|
+
over: The item of the tree that the pointer is over.
|
|
44
|
+
|
|
45
|
+
Returns:
|
|
46
|
+
The folder it lands in, None when it lands nowhere.
|
|
47
|
+
"""
|
|
48
|
+
target = folder_at(project.tree, over)
|
|
49
|
+
if target is None or dragged == project.tree.path:
|
|
50
|
+
return None
|
|
51
|
+
if target.path == dragged or dragged in target.path.parents:
|
|
52
|
+
return None
|
|
53
|
+
if target.path == dragged.parent:
|
|
54
|
+
return None
|
|
55
|
+
return Drop(target.path, None)
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
def note_place(project: Project, over: Path,
|
|
59
|
+
lower: bool) -> Optional[tuple[Folder, int]]:
|
|
60
|
+
"""Return the folder and the place in it that an item stands for.
|
|
61
|
+
|
|
62
|
+
The upper half of a note is the place of that note and the lower
|
|
63
|
+
half the place after it, a folder is the place after its last
|
|
64
|
+
note, and the lower half of a template is the place of the first
|
|
65
|
+
note of its folder. The upper half of a template is above every
|
|
66
|
+
note of the folder, where no note can go.
|
|
67
|
+
|
|
68
|
+
Args:
|
|
69
|
+
project: The project as the tree is showing it.
|
|
70
|
+
over: The item of the tree that the pointer is over.
|
|
71
|
+
lower: Whether the pointer is in the lower half of that item.
|
|
72
|
+
|
|
73
|
+
Returns:
|
|
74
|
+
The folder the note lands in and the place it lands at, None
|
|
75
|
+
when the item stands for no place at all.
|
|
76
|
+
"""
|
|
77
|
+
target = folder_at(project.tree, over)
|
|
78
|
+
if target is not None:
|
|
79
|
+
return (target, len(target.notes))
|
|
80
|
+
holder = folder_at(project.tree, over.parent)
|
|
81
|
+
if holder is None:
|
|
82
|
+
return None
|
|
83
|
+
if over == holder.template:
|
|
84
|
+
return (holder, 0) if lower else None
|
|
85
|
+
if over in holder.notes:
|
|
86
|
+
return (holder, holder.notes.index(over) + int(lower))
|
|
87
|
+
return None
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
def stays_put(holder: Folder, dragged: Path, index: int) -> bool:
|
|
91
|
+
"""Return whether a place is where a note is standing already.
|
|
92
|
+
|
|
93
|
+
A note dropped upon itself, and a note dropped where it would
|
|
94
|
+
land between the notes it is already between, has not moved, so
|
|
95
|
+
there is nothing to do and nothing to show either.
|
|
96
|
+
|
|
97
|
+
Args:
|
|
98
|
+
holder: The folder that the note would land in.
|
|
99
|
+
dragged: The note that is being dragged.
|
|
100
|
+
index: The place it would land at.
|
|
101
|
+
|
|
102
|
+
Returns:
|
|
103
|
+
Whether the note is at that place already.
|
|
104
|
+
"""
|
|
105
|
+
if dragged not in holder.notes:
|
|
106
|
+
return False
|
|
107
|
+
was = holder.notes.index(dragged)
|
|
108
|
+
return index in (was, was + 1)
|
|
109
|
+
|
|
110
|
+
|
|
111
|
+
def dragged_note(project: Project, dragged: Path) -> bool:
|
|
112
|
+
"""Return whether an item of the tree is a note that can be dragged.
|
|
113
|
+
|
|
114
|
+
The tree is asked rather than the name, so that the template of a
|
|
115
|
+
folder, which is no note of the order of that folder, and a row
|
|
116
|
+
that another program has taken away under the tree, are both left
|
|
117
|
+
where they are.
|
|
118
|
+
|
|
119
|
+
Args:
|
|
120
|
+
project: The project as the tree is showing it.
|
|
121
|
+
dragged: The item that is being dragged.
|
|
122
|
+
|
|
123
|
+
Returns:
|
|
124
|
+
Whether the project holds it as a note of one of its folders.
|
|
125
|
+
"""
|
|
126
|
+
holder = folder_at(project.tree, dragged.parent)
|
|
127
|
+
return holder is not None and dragged in holder.notes
|
|
128
|
+
|
|
129
|
+
|
|
130
|
+
def note_drop(project: Project, dragged: Path, over: Path,
|
|
131
|
+
lower: bool) -> Optional[Drop]:
|
|
132
|
+
"""Return where a dragged note lands, None when it cannot land.
|
|
133
|
+
|
|
134
|
+
Args:
|
|
135
|
+
project: The project as the tree is showing it.
|
|
136
|
+
dragged: The note that is being dragged.
|
|
137
|
+
over: The item of the tree that the pointer is over.
|
|
138
|
+
lower: Whether the pointer is in the lower half of that item.
|
|
139
|
+
|
|
140
|
+
Returns:
|
|
141
|
+
The folder and the place it lands at, None for nowhere.
|
|
142
|
+
"""
|
|
143
|
+
place = note_place(project, over, lower)
|
|
144
|
+
if place is None:
|
|
145
|
+
return None
|
|
146
|
+
holder, index = place
|
|
147
|
+
if stays_put(holder, dragged, index):
|
|
148
|
+
return None
|
|
149
|
+
return Drop(holder.path, index)
|
|
150
|
+
|
|
151
|
+
|
|
152
|
+
def drop_target(project: Project, dragged: Path, over: Optional[Path],
|
|
153
|
+
lower: bool) -> Optional[Drop]:
|
|
154
|
+
"""Return where what is dragged lands, None when it cannot land.
|
|
155
|
+
|
|
156
|
+
A note and a folder of the project are dragged, and everything
|
|
157
|
+
else the tree shows stays where it is: every folder has one
|
|
158
|
+
template, and it is no note of the order of that folder.
|
|
159
|
+
|
|
160
|
+
Args:
|
|
161
|
+
project: The project as the tree is showing it.
|
|
162
|
+
dragged: The item that is being dragged.
|
|
163
|
+
over: The item of the tree that the pointer is over, None
|
|
164
|
+
when the pointer is over no item of it at all.
|
|
165
|
+
lower: Whether the pointer is in the lower half of that item.
|
|
166
|
+
|
|
167
|
+
Returns:
|
|
168
|
+
Where it lands, None when it lands nowhere.
|
|
169
|
+
"""
|
|
170
|
+
if over is None:
|
|
171
|
+
return None
|
|
172
|
+
if folder_at(project.tree, dragged) is not None:
|
|
173
|
+
return folder_drop(project, dragged, over)
|
|
174
|
+
if dragged_note(project, dragged):
|
|
175
|
+
return note_drop(project, dragged, over, lower)
|
|
176
|
+
return None
|
|
177
|
+
|
|
178
|
+
|
|
179
|
+
def drop_item(item: Path, drop: Drop) -> Path:
|
|
180
|
+
"""Move a note or a folder to where it was dropped.
|
|
181
|
+
|
|
182
|
+
Args:
|
|
183
|
+
item: The note or the folder that was dragged.
|
|
184
|
+
drop: Where it was dropped, as the tree worked it out.
|
|
185
|
+
|
|
186
|
+
Returns:
|
|
187
|
+
The note or the folder where it now is.
|
|
188
|
+
|
|
189
|
+
Raises:
|
|
190
|
+
NotesmgrError: The move cannot be made, which the message of
|
|
191
|
+
the error says why.
|
|
192
|
+
"""
|
|
193
|
+
if drop.place is None:
|
|
194
|
+
return move_folder(item, drop.folder)
|
|
195
|
+
return move_note(item, drop.folder, drop.place)
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
#! /usr/local/bin/python3
|
|
2
|
+
"""The font that the explorer draws with, and how large it is."""
|
|
3
|
+
|
|
4
|
+
# Copyright (c) 2026 Tom Björkholm
|
|
5
|
+
# MIT License
|
|
6
|
+
|
|
7
|
+
import itertools
|
|
8
|
+
import tkinter
|
|
9
|
+
from tkinter import ttk
|
|
10
|
+
from tkinter.font import Font
|
|
11
|
+
from notesmgr.note_fonts import TEXT_FONT, default_size, family_of, note_size
|
|
12
|
+
|
|
13
|
+
ROW_PADDING = 6
|
|
14
|
+
"""Space in pixels that a row of the tree has beyond its text."""
|
|
15
|
+
|
|
16
|
+
STYLE_KIND = 'Treeview'
|
|
17
|
+
"""What a style of a tree must be called for the tree to take it."""
|
|
18
|
+
|
|
19
|
+
STYLE_NUMBERS = itertools.count()
|
|
20
|
+
"""Numbers that tell the style of one tree from the style of another."""
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
class TreeFont:
|
|
24
|
+
"""The font of one tree, and the style that the tree takes it from.
|
|
25
|
+
|
|
26
|
+
A ttk widget is drawn with the font of its style rather than with
|
|
27
|
+
a font of its own, so the tree is given a style of its own here,
|
|
28
|
+
and drawing it larger or smaller is that one font being resized.
|
|
29
|
+
The rows are made as high as the font needs, so that a name is
|
|
30
|
+
not cut off by a row that stayed as high as it was.
|
|
31
|
+
|
|
32
|
+
The font is the one that Tk draws text with on this system, which
|
|
33
|
+
is where a note starts out as well, so that the tree and the note
|
|
34
|
+
beside it are read in one size.
|
|
35
|
+
"""
|
|
36
|
+
|
|
37
|
+
def __init__(self, widget: tkinter.Misc) -> None:
|
|
38
|
+
"""Make the style of one tree, in the size the system gives.
|
|
39
|
+
|
|
40
|
+
Args:
|
|
41
|
+
widget: The widget that the font and the style belong to.
|
|
42
|
+
"""
|
|
43
|
+
self.style = ttk.Style(widget)
|
|
44
|
+
self.name = f'Notes{next(STYLE_NUMBERS)}.{STYLE_KIND}'
|
|
45
|
+
self.normal = default_size(widget)
|
|
46
|
+
self.size = self.normal
|
|
47
|
+
self.font = Font(widget, family=family_of(TEXT_FONT, widget),
|
|
48
|
+
size=self.size)
|
|
49
|
+
self.apply()
|
|
50
|
+
|
|
51
|
+
def apply(self) -> None:
|
|
52
|
+
"""Let the tree be drawn with the font as it now stands."""
|
|
53
|
+
self.style.configure(self.name, font=self.font,
|
|
54
|
+
rowheight=self.row_height())
|
|
55
|
+
|
|
56
|
+
def row_height(self) -> int:
|
|
57
|
+
"""Return how high a row is, which the font says."""
|
|
58
|
+
return int(self.font.metrics('linespace')) + ROW_PADDING
|
|
59
|
+
|
|
60
|
+
def resize(self, size: int) -> int:
|
|
61
|
+
"""Draw with a font of another size from now on.
|
|
62
|
+
|
|
63
|
+
Args:
|
|
64
|
+
size: The size to draw the tree in.
|
|
65
|
+
|
|
66
|
+
Returns:
|
|
67
|
+
The size that is drawn in, which is as near the one asked
|
|
68
|
+
for as a tree can be read at.
|
|
69
|
+
"""
|
|
70
|
+
self.size = note_size(size)
|
|
71
|
+
self.font.configure(size=self.size)
|
|
72
|
+
self.apply()
|
|
73
|
+
return self.size
|
|
74
|
+
|
|
75
|
+
def zoom(self, step: int) -> int:
|
|
76
|
+
"""Draw so many steps larger, or smaller for a step below zero."""
|
|
77
|
+
return self.resize(self.size + step)
|
|
78
|
+
|
|
79
|
+
def normal_size(self) -> int:
|
|
80
|
+
"""Draw in the size that the tree started out in."""
|
|
81
|
+
return self.resize(self.normal)
|
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
#! /usr/local/bin/python3
|
|
2
|
+
"""The tree of a project, shown at the left of the main window."""
|
|
3
|
+
|
|
4
|
+
# Copyright (c) 2026 Tom Björkholm
|
|
5
|
+
# MIT License
|
|
6
|
+
|
|
7
|
+
import tkinter
|
|
8
|
+
from pathlib import Path
|
|
9
|
+
from tkinter import ttk
|
|
10
|
+
from typing import Callable, Optional
|
|
11
|
+
from notesmgr.explorer_drag import ExplorerDrag
|
|
12
|
+
from notesmgr.explorer_drop import Drop, drop_target
|
|
13
|
+
from notesmgr.explorer_font import TreeFont
|
|
14
|
+
from notesmgr.project import Folder, Project
|
|
15
|
+
|
|
16
|
+
EXPLORER_WIDTH = 260
|
|
17
|
+
"""Width in pixels that the explorer asks the main window for."""
|
|
18
|
+
|
|
19
|
+
SELECT_EVENT = '<<TreeviewSelect>>'
|
|
20
|
+
"""Tk event saying that another item of the tree is selected now."""
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
class ExplorerTree:
|
|
24
|
+
"""Shows the folders, templates and notes of a project as a tree.
|
|
25
|
+
|
|
26
|
+
Every item of the tree is known by the path of the file or folder
|
|
27
|
+
it stands for, so that what the user selected is a path, and the
|
|
28
|
+
view keeps nothing of its own beside the tree itself.
|
|
29
|
+
"""
|
|
30
|
+
|
|
31
|
+
def __init__(self, parent: tkinter.Misc,
|
|
32
|
+
on_select: Callable[[Optional[Path]], None],
|
|
33
|
+
on_drop: Callable[[Path, Drop], None]) -> None:
|
|
34
|
+
"""Build the tree in a frame of its own inside a parent widget.
|
|
35
|
+
|
|
36
|
+
Args:
|
|
37
|
+
parent: The widget that the explorer is placed in.
|
|
38
|
+
on_select: Told which item is selected, whenever that
|
|
39
|
+
changes, and told None when nothing is selected.
|
|
40
|
+
on_drop: Told what was dragged in the tree and where it
|
|
41
|
+
was dropped, once the user has let go of it.
|
|
42
|
+
"""
|
|
43
|
+
self.on_select = on_select
|
|
44
|
+
self.project: Optional[Project] = None
|
|
45
|
+
self.frame = ttk.Frame(parent, width=EXPLORER_WIDTH)
|
|
46
|
+
self.frame.pack_propagate(False)
|
|
47
|
+
self.font = TreeFont(self.frame)
|
|
48
|
+
self.tree = ttk.Treeview(self.frame, show='tree', selectmode='browse',
|
|
49
|
+
style=self.font.name)
|
|
50
|
+
self.scroll = ttk.Scrollbar(self.frame, orient=tkinter.VERTICAL,
|
|
51
|
+
command=self.tree.yview)
|
|
52
|
+
self.tree.configure(yscrollcommand=self.scroll.set)
|
|
53
|
+
self.scroll.pack(side=tkinter.RIGHT, fill=tkinter.Y)
|
|
54
|
+
self.tree.pack(side=tkinter.LEFT, fill=tkinter.BOTH, expand=True)
|
|
55
|
+
self.tree.bind(SELECT_EVENT, self.selection_changed)
|
|
56
|
+
self.drag = ExplorerDrag(self.tree, self.drop_at, on_drop)
|
|
57
|
+
|
|
58
|
+
def selection_changed(self, _event: 'tkinter.Event[ttk.Treeview]') -> None:
|
|
59
|
+
"""Tell whoever is listening what is selected now."""
|
|
60
|
+
self.on_select(self.selected_path())
|
|
61
|
+
|
|
62
|
+
def selected_path(self) -> Optional[Path]:
|
|
63
|
+
"""Return what the user selected, None when nothing is."""
|
|
64
|
+
selection = self.tree.selection()
|
|
65
|
+
return Path(selection[0]) if selection else None
|
|
66
|
+
|
|
67
|
+
def select(self, path: Optional[Path]) -> Optional[Path]:
|
|
68
|
+
"""Select one item of the tree, or nothing at all.
|
|
69
|
+
|
|
70
|
+
The item that is selected is also the one that the arrow keys
|
|
71
|
+
move on from, so that the keyboard carries on from what a
|
|
72
|
+
command made or moved rather than from the top of the tree.
|
|
73
|
+
|
|
74
|
+
Args:
|
|
75
|
+
path: What is to be selected, None for nothing at all.
|
|
76
|
+
A path that the tree does not show selects nothing
|
|
77
|
+
either, which is what a note that was taken away or
|
|
78
|
+
renamed by another program leaves behind.
|
|
79
|
+
|
|
80
|
+
Returns:
|
|
81
|
+
What is selected now, None when nothing is.
|
|
82
|
+
"""
|
|
83
|
+
if path is None or not self.tree.exists(str(path)):
|
|
84
|
+
self.tree.selection_set(())
|
|
85
|
+
return None
|
|
86
|
+
self.tree.selection_set(str(path))
|
|
87
|
+
self.tree.focus(str(path))
|
|
88
|
+
self.tree.see(str(path))
|
|
89
|
+
return path
|
|
90
|
+
|
|
91
|
+
def drop_at(self, dragged: Path, over: Optional[Path],
|
|
92
|
+
lower: bool) -> Optional[Drop]:
|
|
93
|
+
"""Return where a dragged item would land, None for nowhere.
|
|
94
|
+
|
|
95
|
+
Args:
|
|
96
|
+
dragged: The item that is being dragged.
|
|
97
|
+
over: The item of the tree that the pointer is over,
|
|
98
|
+
None when it is over no item at all.
|
|
99
|
+
lower: Whether the pointer is in the lower half of it.
|
|
100
|
+
|
|
101
|
+
Returns:
|
|
102
|
+
Where the item lands, None when it lands nowhere, which
|
|
103
|
+
is what every drag does while no project is open.
|
|
104
|
+
"""
|
|
105
|
+
if self.project is None:
|
|
106
|
+
return None
|
|
107
|
+
return drop_target(self.project, dragged, over, lower)
|
|
108
|
+
|
|
109
|
+
def zoom(self, step: int) -> None:
|
|
110
|
+
"""Draw the tree so many steps larger, or smaller below zero."""
|
|
111
|
+
self.font.zoom(step)
|
|
112
|
+
|
|
113
|
+
def zoom_normal(self) -> None:
|
|
114
|
+
"""Draw the tree in the size that it started out in."""
|
|
115
|
+
self.font.normal_size()
|
|
116
|
+
|
|
117
|
+
def show(self, project: Optional[Project]) -> None:
|
|
118
|
+
"""Show a project, or nothing at all when there is none."""
|
|
119
|
+
self.drag.cancel()
|
|
120
|
+
self.project = project
|
|
121
|
+
self.tree.delete(*self.tree.get_children())
|
|
122
|
+
if project is not None:
|
|
123
|
+
self.add_folder(project.tree, '')
|
|
124
|
+
|
|
125
|
+
def add_folder(self, folder: Folder, parent: str) -> None:
|
|
126
|
+
"""Add one folder of the project and everything below it.
|
|
127
|
+
|
|
128
|
+
The subfolders come first, then the template of the folder,
|
|
129
|
+
and after them the notes in the order the note order file
|
|
130
|
+
gives them.
|
|
131
|
+
|
|
132
|
+
Args:
|
|
133
|
+
folder: The folder of the project to add.
|
|
134
|
+
parent: Item the folder is added under, empty for the top.
|
|
135
|
+
"""
|
|
136
|
+
item = self.tree.insert(parent, tkinter.END, iid=str(folder.path),
|
|
137
|
+
text=folder.path.name or str(folder.path),
|
|
138
|
+
open=True)
|
|
139
|
+
for below in folder.folders:
|
|
140
|
+
self.add_folder(below, item)
|
|
141
|
+
for path in self.shown_files(folder):
|
|
142
|
+
self.tree.insert(item, tkinter.END, iid=str(path), text=path.name)
|
|
143
|
+
|
|
144
|
+
@staticmethod
|
|
145
|
+
def shown_files(folder: Folder) -> list[Path]:
|
|
146
|
+
"""Return the template and the notes of a folder, in that order."""
|
|
147
|
+
template = [] if folder.template is None else [folder.template]
|
|
148
|
+
return template + list(folder.notes)
|