Welcome to K230 Vision Module repository
04.Desktop Layout Algorithm
AppManager Desktop Layout Algorithm
AppManager Desktop Layout Algorithm1. Course Objectives2. Core Source Code Analysis2.1 Coordinate Calculation Formula2.2 Page Turning Logic2.3 Touch and Swipe
1. Course Objectives
After applications are loaded, how does the system arrange them neatly on the screen? In this lesson, we will analyze the grid layout algorithm in AppManager and understand how icon coordinates are calculated.
2. Core Source Code Analysis
File Path: src/canmv/port/builtin_py/yahboom/ybMain/main.py
In the AppManager.__init__ method, a set of key layout parameters are defined:
class AppManager: def __init__(self, ...): # ... # Icon layout parameters self.icon_size = 115 # Icon size (115x115 pixels) self.icon_spacing_x = 30 # Horizontal spacing self.icon_spacing_y = 40 # Vertical spacing self.grid_start_x = 40 # Grid starting x coordinate self.grid_start_y = 100 # Grid starting y coordinate (avoid top status bar) self.icons_per_row = 4 # Number of icons per row self.rows_per_page = 1 # Number of rows per page2.1 Coordinate Calculation Formula
Assuming this is the i-th application (counting from 0), how are its coordinates (x, y) calculated?
The standard grid algorithm is as follows:
Calculate row and column numbers:
xxxxxxxxxxrow = i // self.icons_per_row # Which rowcol = i % self.icons_per_row # Which columnCalculate pixel coordinates:
xxxxxxxxxxx = self.grid_start_x + col * (self.icon_size + self.icon_spacing_x)y = self.grid_start_y + row * (self.icon_size + self.icon_spacing_y)
2.2 Page Turning Logic
Here we explain the page turning logic again
The code uses self.current_page and self.page_count.
xxxxxxxxxxself.current_page = 1self.page_count = 1self.apps_per_page = self.icons_per_row * self.rows_per_page # How many Apps can fit per pageIf the number of applications exceeds apps_per_page, the system will automatically create new pages. When swiping to turn pages, it's actually the entire container holding icons (Container) that performs horizontal displacement.
2.3 Touch and Swipe
Swipe detection variables are also defined in AppManager:
xxxxxxxxxx# Add variables needed for swipe page turningself.touch_start_x = 0self.touch_start_y = 0self.is_swiping = Falseself.min_swipe_distance = 3In other words, AppManager is not only responsible for static layout but also takes over global touch events. When it detects that the horizontal swipe distance exceeds the threshold, it will trigger page turning animation instead of clicking icons.