rust-rustdesk/flutter/lib/utils/scale.dart
Alessandro De Blasis 482840b8bb
feat(ui): custom scale mode with inline controls and live apply (#13045)
* feat(ui): custom scale mode with inline controls and live apply

Signed-off-by: Alessandro De Blasis <alex@deblasis.net>

* Update flutter/lib/models/model.dart

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Update flutter/lib/desktop/widgets/remote_toolbar.dart

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* refactor(dialog): remove unused showCustomScaleDialog function

Signed-off-by: Alessandro De Blasis <alex@deblasis.net>

* feat(ui): enhance custom scale controls with live updates and improved UI

- Introduced a reactive custom scale percentage using RxInt.
- Added initialization of custom scale from stored options after the widget builds.
- Updated viewStyle method to conditionally display custom controls based on selection.
- Implemented a debouncer for smoother scale adjustments.
- Enhanced slider UI with custom thumb shape and improved button interactions.

This update improves user experience by allowing real-time adjustments to the custom scale settings.

Signed-off-by: Alessandro De Blasis <alex@deblasis.net>

* refactor(remote_toolbar): improve widget lifecycle management and enhance slider dimensions

- Moved initialization of custom scale percentage to initState for better lifecycle handling.
- Updated slider thumb dimensions and layout for improved UI consistency.
- Added dispose method to clean up resources in custom scale controls.

These changes enhance the overall performance and user experience of the remote toolbar.

Signed-off-by: Alessandro De Blasis <alex@deblasis.net>

* feat(remote_toolbar): enhance scroll behavior and improve slider thumb rendering

- Introduced a new state variable to manage scroll enablement based on canvas model changes.
- Updated the return value of the viewStyle method to include the scroll enablement status.
- Refactored the slider thumb shape for better performance and visual consistency.
- Improved the initialization of image overflow detection in the CanvasModel.

These changes enhance the user experience by providing dynamic scroll control and a more responsive UI.

Signed-off-by: Alessandro De Blasis <alex@deblasis.net>

* Update flutter/lib/desktop/widgets/remote_toolbar.dart

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* refactor(scale): introduce utility functions for custom scale management for DRY

- Added a new file `scale.dart` containing utility functions to clamp, parse, and compute custom scale percentages.
- Refactored the `CanvasModel` and `_DisplayMenuState` to utilize the new utility functions for fetching and applying custom scale settings.
- Improved code readability and maintainability by centralizing scale-related logic.

These changes enhance the handling of custom scale settings across the application.

Signed-off-by: Alessandro De Blasis alex@deblasis.net
Signed-off-by: Alessandro De Blasis <alex@deblasis.net>

* Update flutter/lib/utils/scale.dart

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Update flutter/lib/models/model.dart

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Update flutter/lib/desktop/widgets/remote_toolbar.dart

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Update flutter/lib/desktop/widgets/remote_toolbar.dart

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Update flutter/lib/desktop/widgets/remote_toolbar.dart

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Update flutter/lib/models/model.dart

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* chore: Remove unused import of 'uuid' in scale.dart

Signed-off-by: Alessandro De Blasis alex@deblasis.net
Signed-off-by: Alessandro De Blasis <alex@deblasis.net>

* feat(remote_toolbar): implement nonlinear mapping for custom scale slider

- Added piecewise mapping functions to convert normalized slider positions to custom scale percentages and vice versa.
- Introduced snapping behavior for the slider to enhance user experience.
- Updated the slider's minimum and maximum values to align with the new mapping logic.
- Adjusted the clamping function to ensure the minimum percentage is 10.

These changes improve the precision and usability of the custom scale slider in the remote toolbar.

Signed-off-by: Alessandro De Blasis <alex@deblasis.net>

* fix(scale): update minimum scale percentage to 5

- Adjusted the minimum scale percentage in both the remote toolbar and the clamping function to improve consistency and usability.
- This change aligns the clamping logic with the updated minimum value for the custom scale slider.

Signed-off-by: Alessandro De Blasis <alex@deblasis.net>

* Update flutter/lib/desktop/widgets/remote_toolbar.dart

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* refactor(scale): centralize custom scale constants in consts.dart

- Moved piecewise mapping constants for the custom scale slider from the remote toolbar to consts.dart for better organization and maintainability.
- Introduced additional constants related to custom scale behavior, including minimum, pivot, and maximum percentages, as well as debounce duration.
- Updated the remote toolbar to reference these centralized constants, improving code clarity and reducing duplication.

These changes enhance the structure and readability of the custom scale implementation.

Signed-off-by: Alessandro De Blasis <alex@deblasis.net>

* refactor(consts): remove duplicate custom scale percent key definition

- Eliminated redundant declaration of the custom scale percent key in consts.dart, ensuring a single source of truth for this constant.
- This change improves code clarity and maintainability by reducing duplication.

Signed-off-by: Alessandro De Blasis <alex@deblasis.net>

* refactor(scale): update clamping logic to use centralized constants

- Modified the clamping function to utilize the newly defined constants for minimum and maximum scale percentages, enhancing code maintainability and clarity.
- This change ensures consistency across the application by referencing a single source for scale limits.

Signed-off-by: Alessandro De Blasis <alex@deblasis.net>

* Enhance RdoMenuButton behavior for custom scale selection

- Updated the RdoMenuButton to include a new `closeOnActivate` parameter, allowing the submenu to remain open when selecting custom scale options.
- Modified the onChanged callback to conditionally trigger a rebuild when entering custom mode, improving user experience by immediately displaying the slider controls.

These changes streamline the interaction with the custom scale feature in the remote toolbar.

Signed-off-by: Alessandro De Blasis <alex@deblasis.net>

* refactor(toolbar):  _DisplayMenuState to simplify scroll handling

- Removed the _scrollEnabled state variable and its associated logic, streamlining the component's state management.
- Updated the RdoMenuButton onChanged callbacks to directly reference the canvasModel's imageOverflow value, enhancing responsiveness and reducing complexity.

These changes improve code clarity and maintainability in the remote toolbar's display menu.

Signed-off-by: Alessandro De Blasis <alex@deblasis.net>

* feat(lang): Add translations for custom scale features in multiple languages

- Introduced new entries for "Scale custom", "Custom scale slider", "Decrease", and "Increase" in various language files to support the custom scale functionality.
- This update enhances the localization of the application, ensuring users can interact with the custom scale features in their preferred language.

Signed-off-by: Alessandro De Blasis <alex@deblasis.net>

* feat(lang): Add translations for custom scale features in Catalan and Romanian

- Updated language files for Catalan and Romanian to include translations for "Custom scale slider", "Decrease", and "Increase".
- This enhancement improves the localization of the application, allowing users to interact with custom scale features in their native languages.

Signed-off-by: Alessandro De Blasis <alex@deblasis.net>

* fix(model): Correct error logging in getSessionCustomScale method

- Updated the error logging statement in the getSessionCustomScale method to properly interpolate the exception message, improving debugging clarity.
- This change ensures that error messages are more informative, aiding in troubleshooting issues related to session scaling.

Signed-off-by: Alessandro De Blasis <alex@deblasis.net>

* refactor(scale): Simplify clamping logic for custom scale percent

- Updated the clampCustomScalePercent function to use the built-in clamp method, improving code readability and maintainability.
- This change ensures consistent clamping behavior across the application by centralizing the logic for valid scale ranges.

Signed-off-by: Alessandro De Blasis <alex@deblasis.net>

* refactor(scale): Remove unused import for web bridge

- Eliminated the conditional import of the web bridge from scale.dart, as it is no longer necessary. This change helps to clean up the code and improve maintainability by removing unused dependencies.

Signed-off-by: Alessandro De Blasis <alex@deblasis.net>

* chore(model): typo

Signed-off-by: Alessandro De Blasis <alex@deblasis.net>

* Update flutter/lib/desktop/widgets/remote_toolbar.dart

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* chore(toolbar): Clarify precision for scale adjustments in remote toolbar

- Added comments to clarify the use of a wide range of divisions for the scale slider, allowing for ~1% precision increments. This change improves user experience by enabling more precise scale value settings, reducing the need for fine-tuning with +/- buttons.

Signed-off-by: Alessandro De Blasis <alex@deblasis.net>

* Update flutter/lib/models/model.dart

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Update flutter/lib/desktop/widgets/remote_toolbar.dart

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* fix(model): Enhance error logging in getSessionCustomScale method

- Improved error logging by adding stack trace output to debugPrintStack, enhancing debugging capabilities for session scaling issues.
- This change provides clearer insights into errors encountered during scale retrieval, aiding in troubleshooting.

Signed-off-by: Alessandro De Blasis <alex@deblasis.net>

* refactor(toolbar): Simplify custom scale percent retrieval in remote toolbar

- Replaced the previous method of retrieving the custom scale percent with a new function, getSessionCustomScalePercent, enhancing code clarity and maintainability.
- This change streamlines the process of obtaining the scale value, ensuring a more efficient and readable implementation.

Signed-off-by: Alessandro De Blasis <alex@deblasis.net>

* Update flutter/lib/desktop/widgets/remote_toolbar.dart

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

---------

Signed-off-by: Alessandro De Blasis <alex@deblasis.net>
Signed-off-by: Alessandro De Blasis alex@deblasis.net
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: RustDesk <71636191+rustdesk@users.noreply.github.com>
2025-10-08 14:40:20 +08:00

34 lines
1.4 KiB
Dart

import 'package:flutter_hbb/consts.dart';
import 'package:flutter_hbb/models/platform_model.dart';
import 'package:uuid/uuid.dart';
/// Clamp custom scale percent to supported bounds.
/// Keep this in sync with the slider's minimum in the desktop toolbar UI.
///
/// This function exists to ensure consistent clamping behavior across the app
/// and to provide a single point of reference for the valid scale range.
int clampCustomScalePercent(int percent) {
return percent.clamp(kScaleCustomMinPercent, kScaleCustomMaxPercent);
}
/// Parse a string percent and clamp. Defaults to 100 when invalid.
int parseCustomScalePercent(String? s, {int defaultPercent = 100}) {
final parsed = int.tryParse(s ?? '') ?? defaultPercent;
return clampCustomScalePercent(parsed);
}
/// Convert a percent value to scale factor after clamping.
double percentToScale(int percent) => clampCustomScalePercent(percent) / 100.0;
/// Fetch, parse and clamp the custom scale percent for a session.
Future<int> getSessionCustomScalePercent(UuidValue sessionId) async {
final opt = await bind.sessionGetFlutterOption(
sessionId: sessionId, k: kCustomScalePercentKey);
return parseCustomScalePercent(opt);
}
/// Fetch and compute the custom scale factor for a session.
Future<double> getSessionCustomScale(UuidValue sessionId) async {
final p = await getSessionCustomScalePercent(sessionId);
return percentToScale(p);
}