button.py 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. # Copyright © 2024 Noah Vogt <noah@noahvogt.com>
  2. # This program is free software: you can redistribute it and/or modify
  3. # it under the terms of the GNU General Public License as published by
  4. # the Free Software Foundation, either version 3 of the License, or
  5. # (at your option) any later version.
  6. # This program is distributed in the hope that it will be useful,
  7. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. # GNU General Public License for more details.
  10. # You should have received a copy of the GNU General Public License
  11. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  12. from typing import override
  13. from kivy.uix.button import Button
  14. from kivy.properties import ( # pylint: disable=no-name-in-module
  15. ListProperty,
  16. NumericProperty,
  17. )
  18. class AutoResizeButton(Button):
  19. """A button that adjusts its label font size dynamically."""
  20. original_pos = ListProperty([0, 0])
  21. state_id = NumericProperty(0)
  22. @override
  23. def __init__(self, **kwargs):
  24. super().__init__(**kwargs)
  25. self.dragged = False
  26. # pylint: disable=no-member
  27. self.bind( # type: ignore
  28. size=self.adjust_font_size, text=self.adjust_font_size
  29. )
  30. @override
  31. def adjust_font_size(self, *_):
  32. """Dynamically adjusts font size to fit the button's bounds."""
  33. if not self.text:
  34. return
  35. # Define the minimum and maximum font size
  36. min_font_size = 10
  37. max_font_size = min(self.width, self.height) * 0.5
  38. # Perform a binary search to find the best font size
  39. best_font_size = min_font_size
  40. while min_font_size <= max_font_size:
  41. current_font_size = (min_font_size + max_font_size) // 2
  42. self.font_size = current_font_size
  43. self.texture_update() # Update texture to get new texture_size
  44. # Check if the text fits within the button's bounds
  45. if (
  46. self.texture_size[0] <= self.width * 0.9
  47. and self.texture_size[1] <= self.height * 0.9
  48. ):
  49. best_font_size = current_font_size
  50. min_font_size = current_font_size + 1
  51. else:
  52. max_font_size = current_font_size - 1
  53. # Apply the best font size
  54. self.font_size = best_font_size