Skip to content

Commit

Permalink
Improve sync indicator
Browse files Browse the repository at this point in the history
  • Loading branch information
JustRedTTG committed Jan 9, 2025
1 parent dfab82a commit 924c33c
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 8 deletions.
2 changes: 2 additions & 0 deletions gui/literals.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@
PDF_RENDER_MODES = Literal['cef', 'pymupdf', 'none', 'retry']
NOTEBOOK_RENDER_MODES = Literal['rm_lines_svg_inker']
CONTEXT_BAR_DIRECTIONS = Literal['down', 'right']
SYNC_STAGE_ICON_TYPES = Literal[
'rotate_inverted', 'export_inverted', 'import_inverted', 'pencil_inverted', 'filter_inverted']
42 changes: 39 additions & 3 deletions gui/rendering.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
from gui.preview_handler import PreviewHandler
from gui.screens.viewer import DocumentViewer
from gui.screens.viewer.viewer import CannotRenderDocument
from rm_api import DocumentSyncProgress
from gui.sync_stages import SYNC_STAGE_ICONS
from rm_api import DocumentSyncProgress, STAGE_SYNC

if TYPE_CHECKING:
from gui import GUI
Expand Down Expand Up @@ -385,7 +386,12 @@ def draw_bottom_bar(gui: 'GUI'):
)


def draw_bottom_loading_bar(gui: 'GUI', current: int, total: int, previous_t: float = 0, finish: bool = False):
def draw_bottom_loading_bar(
gui: 'GUI',
current: int, total: int,
previous_t: float = 0,
finish: bool = False, stage: int = STAGE_SYNC
):
draw_bottom_bar(gui)
bottom_bar_rect = get_bottom_bar_rect(gui)
loading_bar_rect = pe.Rect(0, 0, gui.ratios.bottom_loading_bar_width, gui.ratios.bottom_loading_bar_height)
Expand All @@ -396,7 +402,7 @@ def draw_bottom_loading_bar(gui: 'GUI', current: int, total: int, previous_t: fl
pe.draw.rect(Defaults.BUTTON_DISABLED_LIGHT_COLOR, loading_bar_rect, 0,
edge_rounding=gui.ratios.bottom_loading_bar_rounding)

t = current / total
t = (current / total) if total else 0
if t == 0 or t == 1:
smooth_t = t
elif abs(t - previous_t) > 0.01:
Expand All @@ -412,11 +418,41 @@ def draw_bottom_loading_bar(gui: 'GUI', current: int, total: int, previous_t: fl

# Make and show text of current / total
if not finish:
prepend_text = gui.main_menu.texts[f'rm_api_stage_{stage or STAGE_SYNC}']

icon_key = f'{SYNC_STAGE_ICONS[stage or STAGE_SYNC]}'
base_icon: pe.Image = gui.icons[icon_key]
base_icon_rect = pe.Rect(0, 0, *base_icon.size)

if icon_key == 'rotate_inverted':
icon = pe.Image(
pe.pygame.transform.rotate(
base_icon.surface.surface, 360 - gui.main_menu.rotate_angle # Make it rotate clockwise
)
)
icon_rect = pe.Rect(0, 0, *icon.size)
else:
icon = base_icon
icon_rect = base_icon_rect

text = pe.Text(f"{current} / {total}", Defaults.MAIN_MENU_PROGRESS_FONT, gui.ratios.bottom_bar_size,
colors=Defaults.TEXT_COLOR_H)

# Position the texts and icon
text.rect.midright = loading_bar_rect.midleft
text.rect.right -= gui.ratios.bottom_loading_bar_padding

base_icon_rect.midright = text.rect.midleft
base_icon_rect.right -= gui.ratios.bottom_loading_bar_padding

prepend_text.rect.midright = base_icon_rect.midleft
prepend_text.rect.right -= gui.ratios.bottom_loading_bar_padding

icon_rect.center = base_icon_rect.center

text.display()
prepend_text.display()
icon.display(icon_rect.topleft)
else:
icon: pe.Image = gui.icons['cloud_synced_inverted']
icon_rect = pe.Rect(0, 0, *icon.size)
Expand Down
22 changes: 18 additions & 4 deletions gui/screens/main_menu/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from gui.screens.main_menu.context_bars import TopBar, TopBarSelectOne, TopBarSelectMulti, TopBarSelectMove, TopBarTrash
from gui.screens.main_menu.context_menus import SideBar
from gui.screens.main_menu.main_doc_view import MainMenuDocView
from gui.sync_stages import SYNC_STAGE_TEXTS
from rm_api.models import Document
from rm_api.notifications.models import SyncRefresh, FileSyncProgress, NewDocuments, DocumentSyncProgress

Expand Down Expand Up @@ -46,6 +47,8 @@ class MainMenu(pe.ChildContext):
}

SMALL_HEADER_TEXTS = {
f'rm_api_stage_{stage}': stage_text
for stage, stage_text in SYNC_STAGE_TEXTS.items()
}

MAINTAIN_TEXT_KEYS = (
Expand Down Expand Up @@ -76,6 +79,7 @@ def __init__(self, parent: 'GUI'):
self.call_lock = Lock()
self.file_sync_operation = None
self.previous_t = 0
self.rotate_angle = 0
parent.api.add_hook("main_menu_cache_invalidator", self.api_event_hook)
# TODO: Maybe load from settings
self.current_sorting_mode = 'last_modified'
Expand Down Expand Up @@ -106,7 +110,7 @@ def __init__(self, parent: 'GUI'):
self.ratios.main_menu_x_padding, self.ratios.main_menu_top_height + self.ratios.main_menu_top_padding)
for key, text in self.SMALL_HEADER_TEXTS.items():
self.texts[key] = pe.Text(text, Defaults.MAIN_MENU_BAR_FONT, self.ratios.main_menu_bar_size,
(0, 0), Defaults.TEXT_COLOR)
(0, 0), Defaults.TEXT_COLOR_H)

self.context_menus = {}

Expand Down Expand Up @@ -286,8 +290,12 @@ def post_loop(self):

# Draw progress bar for file sync operations
if self.file_sync_operation and not self.file_sync_operation.finished:
self.previous_t = draw_bottom_loading_bar(self.parent_context, self.file_sync_operation.done,
self.file_sync_operation.total, self.previous_t)
self.previous_t = draw_bottom_loading_bar(
self.parent_context, self.file_sync_operation.done,
self.file_sync_operation.total, self.previous_t,
stage=self.file_sync_operation.stage
)
self.update_sync_angle()
return

# Draw sync operation from loader
Expand All @@ -296,9 +304,10 @@ def post_loop(self):
self.previous_t = draw_bottom_loading_bar(self.parent_context, loader.files_loaded, loader.files_to_load,
self.previous_t)
# Update the data if the loader has loaded more files
if loader.loading_feedback + 3 < loader.files_loaded:
if loader.loading_feedback + 3 < loader.files_loaded: # Update menu every 3 files
self.get_items()
loader.loading_feedback = loader.files_loaded
self.update_sync_angle()
elif loader.loading_feedback:
self.get_items()
loader.loading_feedback = 0
Expand Down Expand Up @@ -350,3 +359,8 @@ def rect_calculations(self):

def handle_event(self, event):
self.doc_view.handle_event(event)

def update_sync_angle(self):
self.rotate_angle += 360 * self.delta_time
if self.rotate_angle >= 360:
self.rotate_angle = 0
32 changes: 32 additions & 0 deletions gui/sync_stages.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
from typing import Dict

from gui.literals import SYNC_STAGE_ICON_TYPES
from rm_api.sync_stages import *

SYNC_STAGE_TEXTS = {
STAGE_START: "Starting sync",
STAGE_GET_ROOT: "Getting root",
STAGE_EXPORT_DOCUMENTS: "Exporting documents",
STAGE_DIFF_CHECK_DOCUMENTS: "Checking documents",
STAGE_PREPARE_DATA: "Preparing data",
STAGE_COMPILE_DATA: "Compiling data",
STAGE_PREPARE_ROOT: "Preparing root",
STAGE_PREPARE_OPERATIONS: "Preparing operations",
STAGE_UPLOAD: "Uploading",
STAGE_UPDATE_ROOT: "Updating root",
STAGE_SYNC: "Syncing",
}

SYNC_STAGE_ICONS: Dict[int, SYNC_STAGE_ICON_TYPES] = {
STAGE_START: "pencil_inverted",
STAGE_GET_ROOT: "import_inverted",
STAGE_EXPORT_DOCUMENTS: "export_inverted",
STAGE_DIFF_CHECK_DOCUMENTS: "rotate_inverted",
STAGE_PREPARE_DATA: "export_inverted",
STAGE_COMPILE_DATA: "pencil_inverted",
STAGE_PREPARE_ROOT: "filter_inverted",
STAGE_PREPARE_OPERATIONS: "pencil_inverted",
STAGE_UPLOAD: "rotate_inverted",
STAGE_UPDATE_ROOT: "pencil_inverted",
STAGE_SYNC: "rotate_inverted",
}
2 changes: 1 addition & 1 deletion rm_api

0 comments on commit 924c33c

Please sign in to comment.