diff --git a/sbapp/kivymd/__init__.py b/sbapp/kivymd/__init__.py index f94c63c..fa6cb27 100644 --- a/sbapp/kivymd/__init__.py +++ b/sbapp/kivymd/__init__.py @@ -9,23 +9,16 @@ Is a collection of Material Design compliant widgets for use with, a framework for cross-platform, touch-enabled graphical applications. The project's goal is to approximate Google's `Material Design spec `_ as close as possible without -sacrificing ease of use or application performance. +sacrificing ease of use. This library is a fork of the `KivyMD project -`_ the author of which stopped supporting -this project three years ago. We found the strength and brought this project -to a new level. Currently we're in **beta** status, so things are changing -all the time and we cannot promise any kind of API stability. -However it is safe to vendor now and make use of what's currently available. +`_. We found the strength and brought this +project to a new level. -Join the project! Just fork the project, branch out and submit a pull request -when your patch is ready. If any changes are necessary, we'll guide you -through the steps that need to be done via PR comments or access to your for -may be requested to outright submit them. If you wish to become a project -developer (permission to create branches on the project without forking for -easier collaboration), have at least one PR approved and ask for it. -If you contribute regularly to the project the role may be offered to you -without asking too. +If you wish to become a project developer (permission to create branches on the +project without forking for easier collaboration), have at least one PR +approved and ask for it. If you contribute regularly to the project the role +may be offered to you without asking too. """ import os @@ -33,7 +26,7 @@ import os import kivy from kivy.logger import Logger -__version__ = "1.0.0.dev0" +__version__ = "1.1.0.dev0" """KivyMD version.""" release = False @@ -56,6 +49,9 @@ images_path = os.path.join(path, f"images{os.sep}") uix_path = os.path.join(path, "uix") """Path to uix directory.""" +glsl_path = os.path.join(path, "data", "glsl") +"""Path to glsl directory.""" + _log_message = ( "KivyMD:" + (" Release" if release else "") diff --git a/sbapp/kivymd/_version.py b/sbapp/kivymd/_version.py deleted file mode 100644 index 5faa449..0000000 --- a/sbapp/kivymd/_version.py +++ /dev/null @@ -1,5 +0,0 @@ -# THIS FILE IS GENERATED FROM KIVYMD SETUP.PY -__version__ = '1.0.0.dev0' -__hash__ = '68ec8626a93b0e7f69e48d9755c4af70028f66a2' -__short_hash__ = '68ec862' -__date__ = '2022-07-07' diff --git a/sbapp/kivymd/app.py b/sbapp/kivymd/app.py index abae5f0..5c7b207 100644 --- a/sbapp/kivymd/app.py +++ b/sbapp/kivymd/app.py @@ -45,7 +45,7 @@ import os from kivy.app import App from kivy.lang import Builder from kivy.logger import Logger -from kivy.properties import ObjectProperty +from kivy.properties import ObjectProperty, StringProperty from kivymd.theming import ThemeManager @@ -71,6 +71,16 @@ class MDApp(App, FpsMonitoring): information. """ + icon = StringProperty("kivymd/images/logo/kivymd-icon-512.png") + """ + See :attr:`~kivy.app.App.icon` attribute for more information. + + .. versionadded:: 1.1.0 + + :attr:`icon` is an :class:`~kivy.properties.StringProperty` + adn default to `kivymd/images/logo/kivymd-icon-512.png`. + """ + theme_cls = ObjectProperty() """ Instance of :class:`~ThemeManager` class. diff --git a/sbapp/kivymd/color_definitions.py b/sbapp/kivymd/color_definitions.py index 47a2edf..e4a7de4 100755 --- a/sbapp/kivymd/color_definitions.py +++ b/sbapp/kivymd/color_definitions.py @@ -412,7 +412,7 @@ To demonstrate the shades of the palette, you can run the following code: self.screen = Factory.Root() for name_tab in colors.keys(): - tab = Tab(text=name_tab) + tab = Tab(title=name_tab) self.screen.ids.android_tabs.add_widget(tab) return self.screen @@ -427,7 +427,7 @@ To demonstrate the shades of the palette, you can run the following code: { "viewclass": "ItemColor", "md_bg_color": colors[tab_text][value_color], - "text": value_color, + "title": value_color, } ) diff --git a/sbapp/kivymd/data/glsl/elevation/elevation.frag b/sbapp/kivymd/data/glsl/elevation/elevation.frag new file mode 100644 index 0000000..9f28abf --- /dev/null +++ b/sbapp/kivymd/data/glsl/elevation/elevation.frag @@ -0,0 +1,44 @@ +/* +The shader code has been refactored for the KivyMD library. +You can find the original code of this shaders at the links: + +https://www.shadertoy.com/view/WtdSDs +https://www.shadertoy.com/view/fsdyzB + +Additional thanks to iq for optimizing conditional block for individual +corner radius: +https://iquilezles.org/articles/distfunctions +*/ + +float roundedBoxSDF(vec2 centerPosition, vec2 size, vec4 radius) { + radius.xy = (centerPosition.x > 0.0) ? radius.xy : radius.zw; + radius.x = (centerPosition.y > 0.0) ? radius.x : radius.y; + + vec2 q = abs(centerPosition) - (size - shadow_softness) + radius.x; + return min(max(q.x, q.y), 0.0) + length(max(q, 0.0)) - radius.x; +} + +void mainImage(out vec4 fragColor, in vec2 fragCoord) { + // Smooth the result (free antialiasing). + float edge0 = 0.0; + float smoothedAlpha = 1.0 - smoothstep(0.0, edge0, 1.0); + // Get the resultant shape. + vec4 quadColor = mix( + vec4( + shadow_color[0], + shadow_color[1], + shadow_color[2], + 0.0 + ), + shadow_color, + smoothedAlpha + ); + // Apply a drop shadow effect. + float shadowDistance = roundedBoxSDF( + fragCoord.xy - mouse.xy - (size / 2.0), size / 2.0, shadow_radius + ); + float shadowAlpha = 1.0 - smoothstep( + -shadow_softness, shadow_softness, shadowDistance + ); + fragColor = mix(quadColor, shadow_color, shadowAlpha - smoothedAlpha); +} diff --git a/sbapp/kivymd/data/glsl/elevation/header.frag b/sbapp/kivymd/data/glsl/elevation/header.frag new file mode 100644 index 0000000..acb3a8a --- /dev/null +++ b/sbapp/kivymd/data/glsl/elevation/header.frag @@ -0,0 +1,10 @@ +#ifdef GL_ES + precision highp float; +#endif + +uniform vec4 resolution; +uniform vec4 mouse; +uniform vec2 size; +uniform vec4 shadow_radius; +uniform float shadow_softness; +uniform vec4 shadow_color; diff --git a/sbapp/kivymd/data/glsl/elevation/main.frag b/sbapp/kivymd/data/glsl/elevation/main.frag new file mode 100644 index 0000000..d9b8712 --- /dev/null +++ b/sbapp/kivymd/data/glsl/elevation/main.frag @@ -0,0 +1,10 @@ +vec2 gfc(in vec4 fc) { + vec2 canvas_pos = resolution.zw; + vec2 uv = fc.xy; + uv.y -= canvas_pos.y; + return uv; +} + +void main(void) { + mainImage(gl_FragColor, gfc(gl_FragCoord)); +} diff --git a/sbapp/kivymd/factory_registers.py b/sbapp/kivymd/factory_registers.py index 57efb1a..1a20b01 100644 --- a/sbapp/kivymd/factory_registers.py +++ b/sbapp/kivymd/factory_registers.py @@ -5,11 +5,15 @@ Register KivyMD widgets to use without import. from kivy.factory import Factory register = Factory.register +register("MDScrollView", module="kivymd.uix.scrollview") +register("MDRecycleView", module="kivymd.uix.recycleview") +register("MDResponsiveLayout", module="kivymd.uix.responsivelayout") register("MDSegmentedControl", module="kivymd.uix.segmentedcontrol") register("MDSegmentedControlItem", module="kivymd.uix.segmentedcontrol") register("MDSliverAppbar", module="kivymd.uix.sliverappbar") register("MDSliverAppbarContent", module="kivymd.uix.sliverappbar") register("MDSliverAppbarHeader", module="kivymd.uix.sliverappbar") +register("MDNavigationRailItem", module="kivymd.uix.navigationrail") register("MDNavigationRail", module="kivymd.uix.navigationrail") register("MDNavigationRailFabButton", module="kivymd.uix.navigationrail") register("MDNavigationRailMenuButton", module="kivymd.uix.navigationrail") @@ -29,7 +33,7 @@ register("MDExpansionPanel", module="kivymd.uix.expansionpanel") register("MDExpansionPanelOneLine", module="kivymd.uix.expansionpanel") register("MDExpansionPanelTwoLine", module="kivymd.uix.expansionpanel") register("MDExpansionPanelThreeLine", module="kivymd.uix.expansionpanel") -register("FitImage", module="kivymd.utils.fitimage") +register("FitImage", module="kivymd.uix.fitimage") register("MDBackdrop", module="kivymd.uix.backdrop") register("MDBanner", module="kivymd.uix.banner") register("MDTooltip", module="kivymd.uix.tooltip") @@ -54,10 +58,7 @@ register("MDCard", module="kivymd.uix.card") register("MDSeparator", module="kivymd.uix.card") register("MDSelectionList", module="kivymd.uix.selection") register("MDChip", module="kivymd.uix.chip") -register("MDChooseChip", module="kivymd.uix.chip") register("MDSmartTile", module="kivymd.uix.imagelist") -register("SmartTileWithLabel", module="kivymd.uix.imagelist") -register("SmartTileWithStar", module="kivymd.uix.imagelist") register("MDLabel", module="kivymd.uix.label") register("MDIcon", module="kivymd.uix.label") register("MDList", module="kivymd.uix.list") @@ -65,7 +66,6 @@ register("ILeftBody", module="kivymd.uix.list") register("ILeftBodyTouch", module="kivymd.uix.list") register("IRightBody", module="kivymd.uix.list") register("IRightBodyTouch", module="kivymd.uix.list") -register("ContainerSupport", module="kivymd.uix.list") register("OneLineListItem", module="kivymd.uix.list") register("TwoLineListItem", module="kivymd.uix.list") register("ThreeLineListItem", module="kivymd.uix.list") @@ -99,9 +99,7 @@ register("MDSlider", module="kivymd.uix.slider") register("MDSpinner", module="kivymd.uix.spinner") register("MDTabs", module="kivymd.uix.tab") register("MDTextField", module="kivymd.uix.textfield") -register("MDTextFieldRound", module="kivymd.uix.textfield") register("MDTextFieldRect", module="kivymd.uix.textfield") -register("MDToolbar", module="kivymd.uix.toolbar") register("MDTopAppBar", module="kivymd.uix.toolbar") register("MDBottomAppBar", module="kivymd.uix.toolbar") register("MDDropDownItem", module="kivymd.uix.dropdownitem") diff --git a/sbapp/kivymd/fonts/materialdesignicons-webfont.ttf b/sbapp/kivymd/fonts/materialdesignicons-webfont.ttf index 162b7b6..d685510 100644 Binary files a/sbapp/kivymd/fonts/materialdesignicons-webfont.ttf and b/sbapp/kivymd/fonts/materialdesignicons-webfont.ttf differ diff --git a/sbapp/kivymd/icon_definitions.py b/sbapp/kivymd/icon_definitions.py index 14cd8eb..3941eae 100755 --- a/sbapp/kivymd/icon_definitions.py +++ b/sbapp/kivymd/icon_definitions.py @@ -12,7 +12,7 @@ Themes/Icon Definitions List of icons from materialdesignicons.com. These expanded material design icons are maintained by Austin Andrews (Templarian on Github). -LAST UPDATED: Version 6.9.96 +LAST UPDATED: Version 7.0.96 To preview the icons and their names, you can use the following application: ---------------------------------------------------------------------------- @@ -154,6 +154,8 @@ md_icons = { "account-box-outline": "\U000F0007", "account-cancel": "\U000F12DF", "account-cancel-outline": "\U000F12E0", + "account-card": "\U000F1BA4", + "account-card-outline": "\U000F1BA5", "account-cash": "\U000F1097", "account-cash-outline": "\U000F1098", "account-check": "\U000F0008", @@ -171,6 +173,8 @@ md_icons = { "account-convert-outline": "\U000F1301", "account-cowboy-hat": "\U000F0E9B", "account-cowboy-hat-outline": "\U000F17F3", + "account-credit-card": "\U000F1BA6", + "account-credit-card-outline": "\U000F1BA7", "account-details": "\U000F0631", "account-details-outline": "\U000F1372", "account-edit": "\U000F06BC", @@ -260,6 +264,7 @@ md_icons = { "air-humidifier": "\U000F1099", "air-humidifier-off": "\U000F1466", "air-purifier": "\U000F0D44", + "air-purifier-off": "\U000F1B57", "airbag": "\U000F0BE9", "airballoon": "\U000F001C", "airballoon-outline": "\U000F100B", @@ -480,7 +485,6 @@ md_icons = { "amplifier-off": "\U000F11B5", "anchor": "\U000F0031", "android": "\U000F0032", - "android-messages": "\U000F0D45", "android-studio": "\U000F0034", "angle-acute": "\U000F0937", "angle-obtuse": "\U000F0938", @@ -745,6 +749,7 @@ md_icons = { "arrow-up-thin": "\U000F19B2", "arrow-up-thin-circle-outline": "\U000F1597", "arrow-vertical-lock": "\U000F115C", + "artboard": "\U000F1B9A", "artstation": "\U000F0B5B", "aspect-ratio": "\U000F0A24", "assistant": "\U000F0064", @@ -762,6 +767,7 @@ md_icons = { "attachment-off": "\U000F1AC3", "attachment-plus": "\U000F1AC4", "attachment-remove": "\U000F1AC5", + "atv": "\U000F1B70", "audio-input-rca": "\U000F186B", "audio-input-stereo-minijack": "\U000F186C", "audio-input-xlr": "\U000F186D", @@ -774,6 +780,8 @@ md_icons = { "autorenew": "\U000F006A", "autorenew-off": "\U000F19E7", "av-timer": "\U000F006B", + "awning": "\U000F1B87", + "awning-outline": "\U000F1B88", "aws": "\U000F0E0F", "axe": "\U000F08C8", "axe-battle": "\U000F1842", @@ -987,6 +995,7 @@ md_icons = { "beaker-remove": "\U000F1233", "beaker-remove-outline": "\U000F1234", "bed": "\U000F02E3", + "bed-clock": "\U000F1B94", "bed-double": "\U000F0FD4", "bed-double-outline": "\U000F0FD3", "bed-empty": "\U000F08A0", @@ -1148,10 +1157,11 @@ md_icons = { "book-sync": "\U000F169C", "book-sync-outline": "\U000F16C8", "book-variant": "\U000F00BF", - "book-variant-multiple": "\U000F00BC", "bookmark": "\U000F00C0", + "bookmark-box": "\U000F1B75", "bookmark-box-multiple": "\U000F196C", "bookmark-box-multiple-outline": "\U000F196D", + "bookmark-box-outline": "\U000F1B76", "bookmark-check": "\U000F00C1", "bookmark-check-outline": "\U000F137B", "bookmark-minus": "\U000F09CC", @@ -1339,8 +1349,11 @@ md_icons = { "calendar-account": "\U000F0ED7", "calendar-account-outline": "\U000F0ED8", "calendar-alert": "\U000F0A31", + "calendar-alert-outline": "\U000F1B62", "calendar-arrow-left": "\U000F1134", "calendar-arrow-right": "\U000F1135", + "calendar-badge": "\U000F1B9D", + "calendar-badge-outline": "\U000F1B9E", "calendar-blank": "\U000F00EE", "calendar-blank-multiple": "\U000F1073", "calendar-blank-outline": "\U000F0B66", @@ -1349,26 +1362,40 @@ md_icons = { "calendar-clock": "\U000F00F0", "calendar-clock-outline": "\U000F16E1", "calendar-collapse-horizontal": "\U000F189D", + "calendar-collapse-horizontal-outline": "\U000F1B63", "calendar-cursor": "\U000F157B", + "calendar-cursor-outline": "\U000F1B64", "calendar-edit": "\U000F08A7", + "calendar-edit-outline": "\U000F1B65", "calendar-end": "\U000F166C", + "calendar-end-outline": "\U000F1B66", "calendar-expand-horizontal": "\U000F189E", + "calendar-expand-horizontal-outline": "\U000F1B67", "calendar-export": "\U000F0B24", + "calendar-export-outline": "\U000F1B68", "calendar-filter": "\U000F1A32", "calendar-filter-outline": "\U000F1A33", "calendar-heart": "\U000F09D2", + "calendar-heart-outline": "\U000F1B69", "calendar-import": "\U000F0B25", + "calendar-import-outline": "\U000F1B6A", "calendar-lock": "\U000F1641", + "calendar-lock-open": "\U000F1B5B", + "calendar-lock-open-outline": "\U000F1B5C", "calendar-lock-outline": "\U000F1642", "calendar-minus": "\U000F0D5C", + "calendar-minus-outline": "\U000F1B6B", "calendar-month": "\U000F0E17", "calendar-month-outline": "\U000F0E18", "calendar-multiple": "\U000F00F1", "calendar-multiple-check": "\U000F00F2", "calendar-multiselect": "\U000F0A32", + "calendar-multiselect-outline": "\U000F1B55", "calendar-outline": "\U000F0B67", "calendar-plus": "\U000F00F3", + "calendar-plus-outline": "\U000F1B6C", "calendar-question": "\U000F0692", + "calendar-question-outline": "\U000F1B6D", "calendar-range": "\U000F0679", "calendar-range-outline": "\U000F0B68", "calendar-refresh": "\U000F01E1", @@ -1376,9 +1403,11 @@ md_icons = { "calendar-remove": "\U000F00F4", "calendar-remove-outline": "\U000F0C45", "calendar-search": "\U000F094C", + "calendar-search-outline": "\U000F1B6E", "calendar-star": "\U000F09D3", "calendar-star-outline": "\U000F1B53", "calendar-start": "\U000F166D", + "calendar-start-outline": "\U000F1B6F", "calendar-sync": "\U000F0E8E", "calendar-sync-outline": "\U000F0E8F", "calendar-text": "\U000F00F5", @@ -1496,6 +1525,8 @@ md_icons = { "car-outline": "\U000F14ED", "car-parking-lights": "\U000F0D63", "car-pickup": "\U000F07AA", + "car-search": "\U000F1B8D", + "car-search-outline": "\U000F1B8E", "car-seat": "\U000F0FA4", "car-seat-cooler": "\U000F0FA5", "car-seat-heater": "\U000F0FA6", @@ -1584,6 +1615,7 @@ md_icons = { "cart-minus": "\U000F0D68", "cart-off": "\U000F066B", "cart-outline": "\U000F0111", + "cart-percent": "\U000F1BAE", "cart-plus": "\U000F0112", "cart-remove": "\U000F0D69", "cart-variant": "\U000F15EB", @@ -1969,6 +2001,7 @@ md_icons = { "cloud-tags": "\U000F07B6", "cloud-upload": "\U000F0167", "cloud-upload-outline": "\U000F0B7E", + "clouds": "\U000F1B95", "clover": "\U000F0816", "coach-lamp": "\U000F1020", "coach-lamp-variant": "\U000F1A37", @@ -2128,8 +2161,10 @@ md_icons = { "contrast": "\U000F0195", "contrast-box": "\U000F0196", "contrast-circle": "\U000F0197", + "controller": "\U000F02B4", "controller-classic": "\U000F0B82", "controller-classic-outline": "\U000F0B83", + "controller-off": "\U000F02B5", "cookie": "\U000F0198", "cookie-alert": "\U000F16D0", "cookie-alert-outline": "\U000F16D1", @@ -2278,6 +2313,7 @@ md_icons = { "currency-sign": "\U000F07BE", "currency-try": "\U000F01B2", "currency-twd": "\U000F07BF", + "currency-uah": "\U000F1B9B", "currency-usd": "\U000F01C1", "currency-usd-off": "\U000F067A", "current-ac": "\U000F1480", @@ -2391,8 +2427,6 @@ md_icons = { "desk-lamp-on": "\U000F1B20", "deskphone": "\U000F01C3", "desktop-classic": "\U000F07C0", - "desktop-mac": "\U000F01C4", - "desktop-mac-dashboard": "\U000F09E8", "desktop-tower": "\U000F01C5", "desktop-tower-monitor": "\U000F0AAB", "details": "\U000F01C6", @@ -2442,7 +2476,6 @@ md_icons = { "disc": "\U000F05EE", "disc-alert": "\U000F01D1", "disc-player": "\U000F0960", - "discord": "\U000F066F", "dishwasher": "\U000F0AAC", "dishwasher-alert": "\U000F11B8", "dishwasher-off": "\U000F11B9", @@ -2457,8 +2490,9 @@ md_icons = { "diving": "\U000F1977", "diving-flippers": "\U000F0DBF", "diving-helmet": "\U000F0DC0", - "diving-scuba": "\U000F0DC1", + "diving-scuba": "\U000F1B77", "diving-scuba-flag": "\U000F0DC2", + "diving-scuba-mask": "\U000F0DC1", "diving-scuba-tank": "\U000F0DC3", "diving-scuba-tank-multiple": "\U000F0DC4", "diving-snorkel": "\U000F0DC5", @@ -2590,6 +2624,10 @@ md_icons = { "email": "\U000F01EE", "email-alert": "\U000F06CF", "email-alert-outline": "\U000F0D42", + "email-arrow-left": "\U000F10DA", + "email-arrow-left-outline": "\U000F10DB", + "email-arrow-right": "\U000F10DC", + "email-arrow-right-outline": "\U000F10DD", "email-box": "\U000F0D03", "email-check": "\U000F0AB1", "email-check-outline": "\U000F0AB2", @@ -2598,6 +2636,7 @@ md_icons = { "email-fast": "\U000F186F", "email-fast-outline": "\U000F1870", "email-lock": "\U000F01F1", + "email-lock-outline": "\U000F1B61", "email-mark-as-unread": "\U000F0B92", "email-minus": "\U000F0EE5", "email-minus-outline": "\U000F0EE6", @@ -2613,16 +2652,12 @@ md_icons = { "email-outline": "\U000F01F0", "email-plus": "\U000F09EB", "email-plus-outline": "\U000F09EC", - "email-receive": "\U000F10DA", - "email-receive-outline": "\U000F10DB", "email-remove": "\U000F1661", "email-remove-outline": "\U000F1662", "email-seal": "\U000F195B", "email-seal-outline": "\U000F195C", "email-search": "\U000F0961", "email-search-outline": "\U000F0962", - "email-send": "\U000F10DC", - "email-send-outline": "\U000F10DD", "email-sync": "\U000F12C7", "email-sync-outline": "\U000F12C8", "email-variant": "\U000F05F0", @@ -3013,7 +3048,15 @@ md_icons = { "flag-remove-outline": "\U000F10B4", "flag-triangle": "\U000F023F", "flag-variant": "\U000F0240", + "flag-variant-minus": "\U000F1BB4", + "flag-variant-minus-outline": "\U000F1BB5", + "flag-variant-off": "\U000F1BB0", + "flag-variant-off-outline": "\U000F1BB1", "flag-variant-outline": "\U000F023E", + "flag-variant-plus": "\U000F1BB2", + "flag-variant-plus-outline": "\U000F1BB3", + "flag-variant-remove": "\U000F1BB6", + "flag-variant-remove-outline": "\U000F1BB7", "flare": "\U000F0D72", "flash": "\U000F0241", "flash-alert": "\U000F0EF7", @@ -3275,6 +3318,7 @@ md_icons = { "format-list-checkbox": "\U000F096A", "format-list-checks": "\U000F0756", "format-list-group": "\U000F1860", + "format-list-group-plus": "\U000F1B56", "format-list-numbered": "\U000F027B", "format-list-numbered-rtl": "\U000F0D0D", "format-list-text": "\U000F126F", @@ -3285,6 +3329,8 @@ md_icons = { "format-paragraph": "\U000F027D", "format-paragraph-spacing": "\U000F1AFD", "format-pilcrow": "\U000F06D8", + "format-pilcrow-arrow-left": "\U000F0286", + "format-pilcrow-arrow-right": "\U000F0285", "format-quote-close": "\U000F027E", "format-quote-close-outline": "\U000F11A8", "format-quote-open": "\U000F0757", @@ -3310,8 +3356,6 @@ md_icons = { "format-text-wrapping-overflow": "\U000F0D0F", "format-text-wrapping-wrap": "\U000F0D10", "format-textbox": "\U000F0D11", - "format-textdirection-l-to-r": "\U000F0285", - "format-textdirection-r-to-l": "\U000F0286", "format-title": "\U000F05F4", "format-underline": "\U000F0287", "format-underline-wavy": "\U000F18E9", @@ -3487,7 +3531,8 @@ md_icons = { "glass-tulip": "\U000F02A8", "glass-wine": "\U000F0876", "glasses": "\U000F02AA", - "globe-light": "\U000F12D7", + "globe-light": "\U000F066F", + "globe-light-outline": "\U000F12D7", "globe-model": "\U000F08E9", "gmail": "\U000F02AB", "gnome": "\U000F02AC", @@ -3512,15 +3557,12 @@ md_icons = { "google-circles-group": "\U000F02B3", "google-classroom": "\U000F02C0", "google-cloud": "\U000F11F6", - "google-controller": "\U000F02B4", - "google-controller-off": "\U000F02B5", "google-downasaur": "\U000F1362", "google-drive": "\U000F02B6", "google-earth": "\U000F02B7", "google-fit": "\U000F096C", "google-glass": "\U000F02B8", "google-hangouts": "\U000F02C9", - "google-home": "\U000F0824", "google-keep": "\U000F06DC", "google-lens": "\U000F09F6", "google-maps": "\U000F05F5", @@ -3582,6 +3624,7 @@ md_icons = { "hand-clap-off": "\U000F1A42", "hand-coin": "\U000F188F", "hand-coin-outline": "\U000F1890", + "hand-cycle": "\U000F1B9C", "hand-extended": "\U000F18B6", "hand-extended-outline": "\U000F18B7", "hand-front-left": "\U000F182B", @@ -3615,6 +3658,7 @@ md_icons = { "harddisk-remove": "\U000F104C", "hat-fedora": "\U000F0BA4", "hazard-lights": "\U000F0C89", + "hdmi-port": "\U000F1BB8", "hdr": "\U000F0D7D", "hdr-off": "\U000F0D7E", "head": "\U000F135E", @@ -3768,6 +3812,8 @@ md_icons = { "home-roof": "\U000F112B", "home-search": "\U000F13B0", "home-search-outline": "\U000F13B1", + "home-silo": "\U000F1BA0", + "home-silo-outline": "\U000F1BA1", "home-switch": "\U000F1794", "home-switch-outline": "\U000F1795", "home-thermometer": "\U000F0F54", @@ -3827,6 +3873,7 @@ md_icons = { "human-pregnant": "\U000F05CF", "human-queue": "\U000F1571", "human-scooter": "\U000F11E9", + "human-walker": "\U000F1B71", "human-wheelchair": "\U000F138D", "human-white-cane": "\U000F1981", "humble-bundle": "\U000F0744", @@ -4005,6 +4052,7 @@ md_icons = { "klingon": "\U000F135B", "knife": "\U000F09FB", "knife-military": "\U000F09FC", + "knob": "\U000F1B96", "koala": "\U000F173F", "kodi": "\U000F0314", "kubernetes": "\U000F10FE", @@ -4092,7 +4140,7 @@ md_icons = { "leaf-off": "\U000F12D9", "leak": "\U000F0DD7", "leak-off": "\U000F0DD8", - "lecturn": "\U000F1AF0", + "lectern": "\U000F1AF0", "led-off": "\U000F032B", "led-on": "\U000F032C", "led-outline": "\U000F032D", @@ -4182,6 +4230,8 @@ md_icons = { "lipstick": "\U000F13B5", "liquid-spot": "\U000F1826", "liquor": "\U000F191E", + "list-box": "\U000F1B7B", + "list-box-outline": "\U000F1B7C", "list-status": "\U000F15AB", "litecoin": "\U000F0A61", "loading": "\U000F0772", @@ -4362,6 +4412,8 @@ md_icons = { "message-bookmark-outline": "\U000F15AD", "message-bulleted": "\U000F06A2", "message-bulleted-off": "\U000F06A3", + "message-check": "\U000F1B8A", + "message-check-outline": "\U000F1B8B", "message-cog": "\U000F06F1", "message-cog-outline": "\U000F1172", "message-draw": "\U000F0363", @@ -4410,6 +4462,8 @@ md_icons = { "metronome-tick": "\U000F07DB", "micro-sd": "\U000F07DC", "microphone": "\U000F036C", + "microphone-message": "\U000F050A", + "microphone-message-off": "\U000F050B", "microphone-minus": "\U000F08B3", "microphone-off": "\U000F036D", "microphone-outline": "\U000F036E", @@ -4519,7 +4573,8 @@ md_icons = { "more": "\U000F037B", "mortar-pestle": "\U000F1748", "mortar-pestle-plus": "\U000F03F1", - "mosque": "\U000F1827", + "mosque": "\U000F0D45", + "mosque-outline": "\U000F1827", "mother-heart": "\U000F1314", "mother-nurse": "\U000F0D21", "motion": "\U000F15B2", @@ -4594,8 +4649,11 @@ md_icons = { "movie-star-outline": "\U000F1718", "mower": "\U000F166F", "mower-bag": "\U000F1670", + "mower-bag-on": "\U000F1B60", + "mower-on": "\U000F1B5F", "muffin": "\U000F098C", "multicast": "\U000F1893", + "multimedia": "\U000F1B97", "multiplication": "\U000F0382", "multiplication-box": "\U000F0383", "mushroom": "\U000F07DF", @@ -4624,6 +4682,7 @@ md_icons = { "music-note-eighth-dotted": "\U000F0F71", "music-note-half": "\U000F0389", "music-note-half-dotted": "\U000F0F72", + "music-note-minus": "\U000F1B89", "music-note-off": "\U000F038A", "music-note-off-outline": "\U000F0F73", "music-note-outline": "\U000F0F74", @@ -4846,7 +4905,13 @@ md_icons = { "office-building-cog-outline": "\U000F194A", "office-building-marker": "\U000F1520", "office-building-marker-outline": "\U000F1521", + "office-building-minus": "\U000F1BAA", + "office-building-minus-outline": "\U000F1BAB", "office-building-outline": "\U000F151F", + "office-building-plus": "\U000F1BA8", + "office-building-plus-outline": "\U000F1BA9", + "office-building-remove": "\U000F1BAC", + "office-building-remove-outline": "\U000F1BAD", "oil": "\U000F03C7", "oil-lamp": "\U000F0F19", "oil-level": "\U000F1053", @@ -4967,6 +5032,8 @@ md_icons = { "patio-heater": "\U000F0F80", "patreon": "\U000F0882", "pause": "\U000F03E4", + "pause-box": "\U000F00BC", + "pause-box-outline": "\U000F1B7A", "pause-circle": "\U000F03E5", "pause-circle-outline": "\U000F03E6", "pause-octagon": "\U000F03E7", @@ -5253,6 +5320,7 @@ md_icons = { "progress-clock": "\U000F0996", "progress-close": "\U000F110A", "progress-download": "\U000F0997", + "progress-helper": "\U000F1BA2", "progress-pencil": "\U000F1787", "progress-question": "\U000F1522", "progress-star": "\U000F1788", @@ -5351,12 +5419,14 @@ md_icons = { "razor-single-edge": "\U000F1998", "react": "\U000F0708", "read": "\U000F0447", - "receipt": "\U000F0449", - "receipt-outline": "\U000F19DC", + "receipt": "\U000F0824", + "receipt-outline": "\U000F04F7", + "receipt-text": "\U000F0449", "receipt-text-check": "\U000F1A63", "receipt-text-check-outline": "\U000F1A64", "receipt-text-minus": "\U000F1A65", "receipt-text-minus-outline": "\U000F1A66", + "receipt-text-outline": "\U000F19DC", "receipt-text-plus": "\U000F1A67", "receipt-text-plus-outline": "\U000F1A68", "receipt-text-remove": "\U000F1A69", @@ -5495,7 +5565,9 @@ md_icons = { "robot-off-outline": "\U000F167B", "robot-outline": "\U000F167A", "robot-vacuum": "\U000F070D", + "robot-vacuum-alert": "\U000F1B5D", "robot-vacuum-variant": "\U000F0908", + "robot-vacuum-variant-alert": "\U000F1B5E", "rocket": "\U000F0463", "rocket-launch": "\U000F14DE", "rocket-launch-outline": "\U000F14DF", @@ -5633,6 +5705,8 @@ md_icons = { "segment": "\U000F0ECB", "select": "\U000F0485", "select-all": "\U000F0486", + "select-arrow-down": "\U000F1B59", + "select-arrow-up": "\U000F1B58", "select-color": "\U000F0D31", "select-compare": "\U000F0AD9", "select-drag": "\U000F0A6C", @@ -5803,6 +5877,7 @@ md_icons = { "sign-pole": "\U000F14F8", "sign-real-estate": "\U000F1118", "sign-text": "\U000F0782", + "sign-yield": "\U000F1BAF", "signal": "\U000F04A2", "signal-2g": "\U000F0712", "signal-3g": "\U000F0713", @@ -5821,7 +5896,8 @@ md_icons = { "signature-freehand": "\U000F0DFC", "signature-image": "\U000F0DFD", "signature-text": "\U000F0DFE", - "silo": "\U000F0B49", + "silo": "\U000F1B9F", + "silo-outline": "\U000F0B49", "silverware": "\U000F04A3", "silverware-clean": "\U000F0FDE", "silverware-fork": "\U000F04A4", @@ -6000,6 +6076,9 @@ md_icons = { "speaker-message": "\U000F1B11", "speaker-multiple": "\U000F0D38", "speaker-off": "\U000F04C4", + "speaker-pause": "\U000F1B73", + "speaker-play": "\U000F1B72", + "speaker-stop": "\U000F1B74", "speaker-wireless": "\U000F071F", "spear": "\U000F1845", "speedometer": "\U000F04C5", @@ -6149,7 +6228,17 @@ md_icons = { "store-settings": "\U000F18D4", "store-settings-outline": "\U000F18D5", "storefront": "\U000F07C7", + "storefront-check": "\U000F1B7D", + "storefront-check-outline": "\U000F1B7E", + "storefront-edit": "\U000F1B7F", + "storefront-edit-outline": "\U000F1B80", + "storefront-minus": "\U000F1B83", + "storefront-minus-outline": "\U000F1B84", "storefront-outline": "\U000F10C1", + "storefront-plus": "\U000F1B81", + "storefront-plus-outline": "\U000F1B82", + "storefront-remove": "\U000F1B85", + "storefront-remove-outline": "\U000F1B86", "stove": "\U000F04DE", "strategy": "\U000F11D6", "stretch-to-page": "\U000F0F2B", @@ -6239,6 +6328,7 @@ md_icons = { "table-edit": "\U000F04F0", "table-eye": "\U000F1094", "table-eye-off": "\U000F13C3", + "table-filter": "\U000F1B8C", "table-furniture": "\U000F05BC", "table-headers-eye": "\U000F121D", "table-headers-eye-off": "\U000F121E", @@ -6272,7 +6362,6 @@ md_icons = { "table-sync": "\U000F13A1", "table-tennis": "\U000F0E68", "tablet": "\U000F04F6", - "tablet-android": "\U000F04F7", "tablet-cellphone": "\U000F09A7", "tablet-dashboard": "\U000F0ECE", "taco": "\U000F0762", @@ -6377,8 +6466,6 @@ md_icons = { "text-search-variant": "\U000F1A7E", "text-shadow": "\U000F0669", "text-short": "\U000F09A9", - "text-to-speech": "\U000F050A", - "text-to-speech-off": "\U000F050B", "texture": "\U000F050C", "texture-box": "\U000F0FE6", "theater": "\U000F050D", @@ -6429,13 +6516,13 @@ md_icons = { "timeline-check-outline": "\U000F1533", "timeline-clock": "\U000F11FB", "timeline-clock-outline": "\U000F11FC", - "timeline-help": "\U000F0F99", - "timeline-help-outline": "\U000F0F9A", "timeline-minus": "\U000F1534", "timeline-minus-outline": "\U000F1535", "timeline-outline": "\U000F0BD2", "timeline-plus": "\U000F0F96", "timeline-plus-outline": "\U000F0F97", + "timeline-question": "\U000F0F99", + "timeline-question-outline": "\U000F0F9A", "timeline-remove": "\U000F1536", "timeline-remove-outline": "\U000F1537", "timeline-text": "\U000F0BD3", @@ -6601,6 +6688,7 @@ md_icons = { "transition-masked": "\U000F0916", "translate": "\U000F05CA", "translate-off": "\U000F0E06", + "translate-variant": "\U000F1B99", "transmission-tower": "\U000F0D3E", "transmission-tower-export": "\U000F192C", "transmission-tower-import": "\U000F192D", @@ -6744,7 +6832,11 @@ md_icons = { "vector-intersection": "\U000F055D", "vector-line": "\U000F055E", "vector-link": "\U000F0FE8", - "vector-point": "\U000F055F", + "vector-point": "\U000F01C4", + "vector-point-edit": "\U000F09E8", + "vector-point-minus": "\U000F1B78", + "vector-point-plus": "\U000F1B79", + "vector-point-select": "\U000F055F", "vector-polygon": "\U000F0560", "vector-polygon-variant": "\U000F1856", "vector-polyline": "\U000F0561", @@ -6912,6 +7004,7 @@ md_icons = { "water-alert-outline": "\U000F1503", "water-boiler": "\U000F0F92", "water-boiler-alert": "\U000F11B3", + "water-boiler-auto": "\U000F1B98", "water-boiler-off": "\U000F11B4", "water-check": "\U000F1504", "water-check-outline": "\U000F1505", @@ -6951,6 +7044,7 @@ md_icons = { "weather-cloudy-alert": "\U000F0F2F", "weather-cloudy-arrow-right": "\U000F0E6E", "weather-cloudy-clock": "\U000F18F6", + "weather-dust": "\U000F1B5A", "weather-fog": "\U000F0591", "weather-hail": "\U000F0592", "weather-hazy": "\U000F0F30", @@ -7072,6 +7166,7 @@ md_icons = { "window-restore": "\U000F05B2", "window-shutter": "\U000F111C", "window-shutter-alert": "\U000F111D", + "window-shutter-auto": "\U000F1BA3", "window-shutter-cog": "\U000F1A8A", "window-shutter-open": "\U000F111E", "window-shutter-settings": "\U000F1A8B", @@ -7084,7 +7179,12 @@ md_icons = { "wrap": "\U000F05B6", "wrap-disabled": "\U000F0BDF", "wrench": "\U000F05B7", + "wrench-check": "\U000F1B8F", + "wrench-check-outline": "\U000F1B90", "wrench-clock": "\U000F19A3", + "wrench-clock-outline": "\U000F1B93", + "wrench-cog": "\U000F1B91", + "wrench-cog-outline": "\U000F1B92", "wrench-outline": "\U000F0BE0", "xamarin": "\U000F0845", "xml": "\U000F05C0", diff --git a/sbapp/kivymd/images/firebase-logo.png b/sbapp/kivymd/images/firebase-logo.png deleted file mode 100644 index 68674e6..0000000 Binary files a/sbapp/kivymd/images/firebase-logo.png and /dev/null differ diff --git a/sbapp/kivymd/images/folder.png b/sbapp/kivymd/images/folder.png index 58fed42..fe0ed5b 100644 Binary files a/sbapp/kivymd/images/folder.png and b/sbapp/kivymd/images/folder.png differ diff --git a/sbapp/kivymd/images/logo/kivymd-icon-128.png b/sbapp/kivymd/images/logo/kivymd-icon-128.png new file mode 100644 index 0000000..289c001 Binary files /dev/null and b/sbapp/kivymd/images/logo/kivymd-icon-128.png differ diff --git a/sbapp/kivymd/images/logo/kivymd-icon-256.png b/sbapp/kivymd/images/logo/kivymd-icon-256.png new file mode 100644 index 0000000..869b768 Binary files /dev/null and b/sbapp/kivymd/images/logo/kivymd-icon-256.png differ diff --git a/sbapp/kivymd/images/logo/kivymd-icon-512.png b/sbapp/kivymd/images/logo/kivymd-icon-512.png new file mode 100644 index 0000000..82d666d Binary files /dev/null and b/sbapp/kivymd/images/logo/kivymd-icon-512.png differ diff --git a/sbapp/kivymd/images/quad_shadow-0.png b/sbapp/kivymd/images/quad_shadow-0.png deleted file mode 100644 index f847665..0000000 Binary files a/sbapp/kivymd/images/quad_shadow-0.png and /dev/null differ diff --git a/sbapp/kivymd/images/quad_shadow-1.png b/sbapp/kivymd/images/quad_shadow-1.png deleted file mode 100644 index fea057f..0000000 Binary files a/sbapp/kivymd/images/quad_shadow-1.png and /dev/null differ diff --git a/sbapp/kivymd/images/quad_shadow-2.png b/sbapp/kivymd/images/quad_shadow-2.png deleted file mode 100644 index 1949d5e..0000000 Binary files a/sbapp/kivymd/images/quad_shadow-2.png and /dev/null differ diff --git a/sbapp/kivymd/images/quad_shadow.atlas b/sbapp/kivymd/images/quad_shadow.atlas deleted file mode 100644 index 68e0aad..0000000 --- a/sbapp/kivymd/images/quad_shadow.atlas +++ /dev/null @@ -1 +0,0 @@ -{"quad_shadow-1.png": {"20": [2, 136, 128, 128], "21": [132, 136, 128, 128], "22": [262, 136, 128, 128], "23": [2, 6, 128, 128], "19": [132, 266, 128, 128], "18": [2, 266, 128, 128], "1": [262, 266, 128, 128], "3": [262, 6, 128, 128], "2": [132, 6, 128, 128]}, "quad_shadow-0.png": {"11": [262, 266, 128, 128], "10": [132, 266, 128, 128], "13": [132, 136, 128, 128], "12": [2, 136, 128, 128], "15": [2, 6, 128, 128], "14": [262, 136, 128, 128], "17": [262, 6, 128, 128], "16": [132, 6, 128, 128], "0": [2, 266, 128, 128]}, "quad_shadow-2.png": {"5": [132, 266, 128, 128], "4": [2, 266, 128, 128], "7": [2, 136, 128, 128], "6": [262, 266, 128, 128], "9": [262, 136, 128, 128], "8": [132, 136, 128, 128]}} \ No newline at end of file diff --git a/sbapp/kivymd/images/rec_shadow-0.png b/sbapp/kivymd/images/rec_shadow-0.png deleted file mode 100644 index f02b919..0000000 Binary files a/sbapp/kivymd/images/rec_shadow-0.png and /dev/null differ diff --git a/sbapp/kivymd/images/rec_shadow-1.png b/sbapp/kivymd/images/rec_shadow-1.png deleted file mode 100644 index f752fd2..0000000 Binary files a/sbapp/kivymd/images/rec_shadow-1.png and /dev/null differ diff --git a/sbapp/kivymd/images/rec_shadow.atlas b/sbapp/kivymd/images/rec_shadow.atlas deleted file mode 100644 index 71b0e9d..0000000 --- a/sbapp/kivymd/images/rec_shadow.atlas +++ /dev/null @@ -1 +0,0 @@ -{"rec_shadow-1.png": {"20": [2, 266, 256, 128], "21": [260, 266, 256, 128], "22": [518, 266, 256, 128], "23": [776, 266, 256, 128], "3": [260, 136, 256, 128], "2": [2, 136, 256, 128], "5": [776, 136, 256, 128], "4": [518, 136, 256, 128], "7": [260, 6, 256, 128], "6": [2, 6, 256, 128], "9": [776, 6, 256, 128], "8": [518, 6, 256, 128]}, "rec_shadow-0.png": {"11": [518, 266, 256, 128], "10": [260, 266, 256, 128], "13": [2, 136, 256, 128], "12": [776, 266, 256, 128], "15": [518, 136, 256, 128], "14": [260, 136, 256, 128], "17": [2, 6, 256, 128], "16": [776, 136, 256, 128], "19": [518, 6, 256, 128], "18": [260, 6, 256, 128], "1": [776, 6, 256, 128], "0": [2, 266, 256, 128]}} \ No newline at end of file diff --git a/sbapp/kivymd/images/rec_st_shadow-0.png b/sbapp/kivymd/images/rec_st_shadow-0.png deleted file mode 100644 index 887327d..0000000 Binary files a/sbapp/kivymd/images/rec_st_shadow-0.png and /dev/null differ diff --git a/sbapp/kivymd/images/rec_st_shadow-1.png b/sbapp/kivymd/images/rec_st_shadow-1.png deleted file mode 100644 index 759ee65..0000000 Binary files a/sbapp/kivymd/images/rec_st_shadow-1.png and /dev/null differ diff --git a/sbapp/kivymd/images/rec_st_shadow-2.png b/sbapp/kivymd/images/rec_st_shadow-2.png deleted file mode 100644 index e9fdacc..0000000 Binary files a/sbapp/kivymd/images/rec_st_shadow-2.png and /dev/null differ diff --git a/sbapp/kivymd/images/rec_st_shadow.atlas b/sbapp/kivymd/images/rec_st_shadow.atlas deleted file mode 100644 index d4c24ab..0000000 --- a/sbapp/kivymd/images/rec_st_shadow.atlas +++ /dev/null @@ -1 +0,0 @@ -{"rec_st_shadow-0.png": {"11": [262, 138, 128, 256], "10": [132, 138, 128, 256], "13": [522, 138, 128, 256], "12": [392, 138, 128, 256], "15": [782, 138, 128, 256], "14": [652, 138, 128, 256], "16": [912, 138, 128, 256], "0": [2, 138, 128, 256]}, "rec_st_shadow-1.png": {"20": [522, 138, 128, 256], "21": [652, 138, 128, 256], "17": [2, 138, 128, 256], "23": [912, 138, 128, 256], "19": [262, 138, 128, 256], "18": [132, 138, 128, 256], "22": [782, 138, 128, 256], "1": [392, 138, 128, 256]}, "rec_st_shadow-2.png": {"3": [132, 138, 128, 256], "2": [2, 138, 128, 256], "5": [392, 138, 128, 256], "4": [262, 138, 128, 256], "7": [652, 138, 128, 256], "6": [522, 138, 128, 256], "9": [912, 138, 128, 256], "8": [782, 138, 128, 256]}} \ No newline at end of file diff --git a/sbapp/kivymd/images/restdb-logo.png b/sbapp/kivymd/images/restdb-logo.png deleted file mode 100644 index 01adbcc..0000000 Binary files a/sbapp/kivymd/images/restdb-logo.png and /dev/null differ diff --git a/sbapp/kivymd/images/round_shadow-0.png b/sbapp/kivymd/images/round_shadow-0.png deleted file mode 100644 index 26d9840..0000000 Binary files a/sbapp/kivymd/images/round_shadow-0.png and /dev/null differ diff --git a/sbapp/kivymd/images/round_shadow-1.png b/sbapp/kivymd/images/round_shadow-1.png deleted file mode 100644 index d0f4c0f..0000000 Binary files a/sbapp/kivymd/images/round_shadow-1.png and /dev/null differ diff --git a/sbapp/kivymd/images/round_shadow-2.png b/sbapp/kivymd/images/round_shadow-2.png deleted file mode 100644 index d5feef2..0000000 Binary files a/sbapp/kivymd/images/round_shadow-2.png and /dev/null differ diff --git a/sbapp/kivymd/images/round_shadow.atlas b/sbapp/kivymd/images/round_shadow.atlas deleted file mode 100644 index f25016d..0000000 --- a/sbapp/kivymd/images/round_shadow.atlas +++ /dev/null @@ -1 +0,0 @@ -{"round_shadow-1.png": {"20": [2, 136, 128, 128], "21": [132, 136, 128, 128], "22": [262, 136, 128, 128], "23": [2, 6, 128, 128], "19": [132, 266, 128, 128], "18": [2, 266, 128, 128], "1": [262, 266, 128, 128], "3": [262, 6, 128, 128], "2": [132, 6, 128, 128]}, "round_shadow-0.png": {"11": [262, 266, 128, 128], "10": [132, 266, 128, 128], "13": [132, 136, 128, 128], "12": [2, 136, 128, 128], "15": [2, 6, 128, 128], "14": [262, 136, 128, 128], "17": [262, 6, 128, 128], "16": [132, 6, 128, 128], "0": [2, 266, 128, 128]}, "round_shadow-2.png": {"5": [132, 266, 128, 128], "4": [2, 266, 128, 128], "7": [2, 136, 128, 128], "6": [262, 266, 128, 128], "9": [262, 136, 128, 128], "8": [132, 136, 128, 128]}} \ No newline at end of file diff --git a/sbapp/kivymd/tests/base_test.py b/sbapp/kivymd/tests/base_test.py new file mode 100644 index 0000000..9fa52ef --- /dev/null +++ b/sbapp/kivymd/tests/base_test.py @@ -0,0 +1,9 @@ +from kivy.tests.common import GraphicUnitTest + +from kivymd.app import MDApp + + +class BaseTest(GraphicUnitTest): + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + self.app = MDApp() # NOQA diff --git a/sbapp/kivymd/tests/pyinstaller/test_pyinstaller_packaging.py b/sbapp/kivymd/tests/pyinstaller/test_pyinstaller_packaging.py index 801f5bf..3b81fdf 100644 --- a/sbapp/kivymd/tests/pyinstaller/test_pyinstaller_packaging.py +++ b/sbapp/kivymd/tests/pyinstaller/test_pyinstaller_packaging.py @@ -35,8 +35,15 @@ assert "Icons" in LabelBase._fonts.keys() # NOQA images = os.listdir(kivymd.images_path) print(images) +assert "logo" in images +assert "alpha_layer.png" in images +assert "black.png" in images +assert "blue.png" in images +assert "red.png" in images +assert "green.png" in images +assert "yellow.png" in images assert "folder.png" in images -assert "rec_shadow.atlas" in images +assert "transparent.png" in images """ ) pyi_main.run( diff --git a/sbapp/kivymd/tests/test_backdrop.py b/sbapp/kivymd/tests/test_backdrop.py new file mode 100644 index 0000000..85b1c85 --- /dev/null +++ b/sbapp/kivymd/tests/test_backdrop.py @@ -0,0 +1,24 @@ +from kivymd.tests.base_test import BaseTest + + +class BackdropTest(BaseTest): + def test_backdrop_raw_app(self): + from kivymd.uix.backdrop import MDBackdrop + from kivymd.uix.backdrop.backdrop import ( + MDBackdropBackLayer, + MDBackdropFrontLayer, + ) + from kivymd.uix.screen import MDScreen + from kivymd.uix.widget import MDWidget + + self.render( + MDScreen( + MDBackdrop( + MDBackdropBackLayer(MDWidget()), + MDBackdropFrontLayer(MDWidget()), + id="backdrop", + title="Example Backdrop", + header_text="Menu:", + ) + ) + ) diff --git a/sbapp/kivymd/tests/test_bottom_navigation.py b/sbapp/kivymd/tests/test_bottom_navigation.py new file mode 100644 index 0000000..9b3467c --- /dev/null +++ b/sbapp/kivymd/tests/test_bottom_navigation.py @@ -0,0 +1,32 @@ +from kivymd.tests.base_test import BaseTest + + +class BottomNavigationTest(BaseTest): + def test_bottom_navigation_m3_style_raw_app(self): + from kivymd.uix.bottomnavigation import ( + MDBottomNavigation, + MDBottomNavigationItem, + ) + from kivymd.uix.screen import MDScreen + + self.app.theme_cls.material_style = "M3" + self.render( + MDScreen( + MDBottomNavigation( + MDBottomNavigationItem( + name="screen 1", + text="Mail", + icon="gmail", + ), + MDBottomNavigationItem( + name="screen 2", + text="Twitter", + icon="twitter", + badge_icon="numeric-10", + ), + panel_color="#eeeaea", + selected_color_background="#97ecf8", + text_color_active="red", + ) + ) + ) diff --git a/sbapp/kivymd/tests/test_card.py b/sbapp/kivymd/tests/test_card.py new file mode 100644 index 0000000..81dd0dd --- /dev/null +++ b/sbapp/kivymd/tests/test_card.py @@ -0,0 +1,25 @@ +from kivymd.tests.base_test import BaseTest + + +class CardTest(BaseTest): + def test_card_m3_style_raw_app(self): + from kivymd.uix.behaviors import RoundedRectangularElevationBehavior + from kivymd.uix.card import MDCard + from kivymd.uix.screen import MDScreen + + class MD3Card(MDCard, RoundedRectangularElevationBehavior): + pass + + self.app.theme_cls.material_style = "M3" + self.render( + MDScreen( + MD3Card( + size_hint=(None, None), + pos_hint={"center_x": 0.5, "center_y": 0.5}, + size=("200dp", "100dp"), + line_color=(0.2, 0.2, 0.2, 0.8), + style="elevated", + md_bg_color="lightblue", + ) + ) + ) diff --git a/sbapp/kivymd/tests/test_chip.py b/sbapp/kivymd/tests/test_chip.py new file mode 100644 index 0000000..6f30f67 --- /dev/null +++ b/sbapp/kivymd/tests/test_chip.py @@ -0,0 +1,16 @@ +from kivymd.tests.base_test import BaseTest + + +class ChipTest(BaseTest): + def test_chip_raw_app(self): + from kivymd.uix.chip import MDChip + from kivymd.uix.screen import MDScreen + + self.render( + MDScreen( + MDChip( + text="Portland", + pos_hint={"center_x": 0.5, "center_y": 0.5}, + ) + ) + ) diff --git a/sbapp/kivymd/tests/test_create_project.py b/sbapp/kivymd/tests/test_create_project.py index 3122f1e..31265fa 100644 --- a/sbapp/kivymd/tests/test_create_project.py +++ b/sbapp/kivymd/tests/test_create_project.py @@ -1,14 +1,13 @@ def test_create_project(): import os - import sys os.system( - f"{sys.executable} -m kivymd.tools.patterns.create_project " + f"python3.10 -m kivymd.tools.patterns.create_project " f"MVC " f"{os.path.expanduser('~')} " f"TestProject " - f"{sys.executable} " - f"master " + f"python3.10 " + f"stable " f"--name_screen TestProjectScreen " f"--name_database restdb " f"--use_hotreload yes" diff --git a/sbapp/kivymd/tests/test_fitimage.py b/sbapp/kivymd/tests/test_fitimage.py new file mode 100644 index 0000000..de159de --- /dev/null +++ b/sbapp/kivymd/tests/test_fitimage.py @@ -0,0 +1,24 @@ +from kivymd.tests.base_test import BaseTest + + +class FitImageTest(BaseTest): + def test_fitimage_raw_app(self): + import os + + from kivymd import images_path + from kivymd.uix.fitimage import FitImage + from kivymd.uix.screen import MDScreen + + self.render( + MDScreen( + FitImage( + source=os.path.join( + images_path, "logo", "kivymd-icon-512.png" + ), + size_hint=(0.5, 0.5), + pos_hint={"center_x": 0.5, "center_y": 0.5}, + radius=[36, 36, 0, 0], + mipmap=True, + ) + ) + ) diff --git a/sbapp/kivymd/tests/test_imagelist.py b/sbapp/kivymd/tests/test_imagelist.py new file mode 100644 index 0000000..4ca989e --- /dev/null +++ b/sbapp/kivymd/tests/test_imagelist.py @@ -0,0 +1,39 @@ +from kivymd.tests.base_test import BaseTest + + +class ImageListTest(BaseTest): + def test_imagelist_raw_app(self): + import os + + from kivymd import images_path + from kivymd.uix.button import MDIconButton + from kivymd.uix.imagelist import MDSmartTile + from kivymd.uix.label import MDLabel + from kivymd.uix.screen import MDScreen + + self.render( + MDScreen( + MDSmartTile( + MDIconButton( + icon="heart-outline", + theme_icon_color="Custom", + icon_color="red", + pos_hint={"center_y": 0.5}, + ), + MDLabel( + text="Julia and Julie", + bold=True, + color="white", + ), + radius=24, + box_radius=[0, 0, 24, 24], + box_color="grey", + source=os.path.join( + images_path, "logo", "kivymd-icon-512.png" + ), + pos_hint={"center_x": 0.5, "center_y": 0.5}, + size_hint=(None, None), + size=("320dp", "320dp"), + ) + ) + ) diff --git a/sbapp/kivymd/tests/test_list.py b/sbapp/kivymd/tests/test_list.py new file mode 100644 index 0000000..da5a045 --- /dev/null +++ b/sbapp/kivymd/tests/test_list.py @@ -0,0 +1,67 @@ +from kivymd.tests.base_test import BaseTest + + +class ListTest(BaseTest): + def test_list_raw_app(self): + import os + + from kivymd import images_path + from kivymd.uix.list import ( + IconLeftWidget, + IconRightWidget, + ImageLeftWidget, + IRightBodyTouch, + MDList, + OneLineAvatarIconListItem, + OneLineAvatarListItem, + OneLineIconListItem, + OneLineListItem, + ThreeLineListItem, + TwoLineListItem, + ) + from kivymd.uix.screen import MDScreen + from kivymd.uix.scrollview import MDScrollView + from kivymd.uix.selectioncontrol import MDCheckbox + + class RightCheckbox(IRightBodyTouch, MDCheckbox): + pass + + self.render( + MDScreen( + MDScrollView( + MDList( + OneLineListItem(text="Text"), + TwoLineListItem( + text="Text", secondary_text="secondary text" + ), + ThreeLineListItem( + text="Text", + secondary_text="secondary text", + tertiary_text="tertiary text", + ), + OneLineAvatarListItem( + ImageLeftWidget( + source=os.path.join( + images_path, "logo", "kivymd-icon-512.png" + ) + ), + text="Text", + ), + OneLineIconListItem( + IconLeftWidget(icon="plus"), + text="Text", + ), + OneLineAvatarIconListItem( + IconLeftWidget(icon="plus"), + IconRightWidget(icon="minus"), + text="Text", + ), + OneLineAvatarIconListItem( + IconLeftWidget(icon="plus"), + RightCheckbox(), + text="Text", + ), + ) + ) + ) + ) diff --git a/sbapp/kivymd/tests/test_navigationdrawer.py b/sbapp/kivymd/tests/test_navigationdrawer.py new file mode 100644 index 0000000..87039c2 --- /dev/null +++ b/sbapp/kivymd/tests/test_navigationdrawer.py @@ -0,0 +1,94 @@ +from kivymd.tests.base_test import BaseTest + + +class NavigationDrawerTest(BaseTest): + def test_navigationdrawer_raw_app(self): + from kivymd.uix.navigationdrawer import ( + MDNavigationDrawer, + MDNavigationDrawerDivider, + MDNavigationDrawerHeader, + MDNavigationDrawerItem, + MDNavigationDrawerLabel, + MDNavigationDrawerMenu, + MDNavigationLayout, + ) + from kivymd.uix.screen import MDScreen + from kivymd.uix.screenmanager import MDScreenManager + from kivymd.uix.toolbar import MDTopAppBar + + class DrawerClickableItem(MDNavigationDrawerItem): + def __init__(self, **kwargs): + super().__init__(**kwargs) + self.focus_color = "#e7e4c0" + self.unfocus_color = "#f7f4e7" + self.text_color = "#4a4939" + self.icon_color = "#4a4939" + self.ripple_color = "#c5bdd2" + self.selected_color = "#0c6c4d" + + class DrawerLabelItem(MDNavigationDrawerItem): + def __init__(self, **kwargs): + super().__init__(**kwargs) + self.bg_color = "#f7f4e7" + self.text_color = "#4a4939" + self.icon_color = "#4a4939" + _no_ripple_effect = True # NOQA + + self.app.theme_cls.material_style = "M3" + self.render( + MDNavigationLayout( + MDScreenManager( + MDScreen( + MDTopAppBar( + title="Navigation Drawer", + elevation=10, + pos_hint={"top": 1}, + md_bg_color="#e7e4c0", + specific_text_color="#4a4939", + left_action_items=[ + ["menu", lambda x: self.nav_drawer_open()] + ], + ) + ) + ), + MDNavigationDrawer( + MDNavigationDrawerMenu( + MDNavigationDrawerHeader( + title="Header title", + title_color="#4a4939", + text="Header text", + spacing="4dp", + padding=("12dp", 0, 0, "56dp"), + ), + MDNavigationDrawerLabel( + text="Mail", + ), + DrawerClickableItem( + icon="gmail", + right_text="+99", + text_right_color="#4a4939", + text="Inbox", + radius=24, + ), + DrawerClickableItem( + icon="send", + text="Outbox", + radius=24, + ), + MDNavigationDrawerDivider(), + MDNavigationDrawerLabel( + text="Labels", + ), + DrawerLabelItem( + icon="information-outline", + text="Label", + ), + DrawerLabelItem( + icon="information-outline", + text="Label", + ), + ), + id="nav_drawer", + ), + ) + ) diff --git a/sbapp/kivymd/tests/test_tab.py b/sbapp/kivymd/tests/test_tab.py new file mode 100644 index 0000000..1ad5920 --- /dev/null +++ b/sbapp/kivymd/tests/test_tab.py @@ -0,0 +1,14 @@ +from kivymd.tests.base_test import BaseTest + + +class TabTest(BaseTest): + def test_tab_raw_app(self): + from kivymd.uix.floatlayout import MDFloatLayout + from kivymd.uix.tab import MDTabs, MDTabsBase + + class Tab(MDFloatLayout, MDTabsBase): + pass + + tab = MDTabs() + tab.add_widget(Tab(title="Tab")) + self.render(tab) diff --git a/sbapp/kivymd/tests/test_textfield.py b/sbapp/kivymd/tests/test_textfield.py new file mode 100644 index 0000000..b5654e1 --- /dev/null +++ b/sbapp/kivymd/tests/test_textfield.py @@ -0,0 +1,72 @@ +# from kivy.clock import Clock +# from kivy.uix.textinput import TextInput + +from kivymd.tests.base_test import BaseTest + + +class TextFieldTest(BaseTest): + def test_textfield_raw_app(self): + from kivymd.uix.boxlayout import MDBoxLayout + from kivymd.uix.button import MDFlatButton + from kivymd.uix.screen import MDScreen + from kivymd.uix.textfield import MDTextField + + # def set_text(): + # for widget in self.screen.ids.box.children: + # if issubclass(widget.__class__, TextInput): + # widget.text = "Input text" + + self.render( + MDScreen( + MDBoxLayout( + MDTextField( + hint_text="Label", + helper_text="Error massage", + mode="rectangle", + max_text_length=5, + ), + MDTextField( + icon_left="git", + hint_text="Label", + helper_text="Error massage", + mode="rectangle", + ), + MDTextField( + icon_left="git", + hint_text="Label", + helper_text="Error massage", + mode="fill", + ), + MDTextField( + hint_text="Label", + helper_text="Error massage", + mode="fill", + ), + MDTextField( + hint_text="Label", + helper_text="Error massage", + ), + MDTextField( + icon_left="git", + hint_text="Label", + helper_text="Error massage", + ), + MDTextField( + hint_text="Round mode", + mode="round", + max_text_length=15, + helper_text="Massage", + ), + MDFlatButton( + text="SET TEXT", + pos_hint={"center_x": 0.5}, + ), + id="box", + orientation="vertical", + spacing="20dp", + adaptive_height=True, + size_hint_x=0.8, + pos_hint={"center_x": 0.5, "center_y": 0.5}, + ) + ) + ) diff --git a/sbapp/kivymd/theming.py b/sbapp/kivymd/theming.py index 2a7a0c6..7bc4116 100755 --- a/sbapp/kivymd/theming.py +++ b/sbapp/kivymd/theming.py @@ -9,16 +9,18 @@ Themes/Theming Material App ------------ -The main class of your application, which in `Kivy` inherits from the App class, -in `KivyMD` must inherit from the `MDApp` class. The `MDApp` class has -properties that allow you to control application properties -such as :attr:`color/style/font` of interface elements and much more. +The main class of your application, which in `Kivy` inherits from the +:class:`~kivy.app.App` class, in `KivyMD` must inherit from the +:class:`~kivymd.app.MDApp` class. The :class:`~kivymd.app.MDApp` class has +properties that allow you to control application properties such as +:attr:`color/style/font` of interface elements and much more. Control material properties --------------------------- -The main application class inherited from the `MDApp` class has the :attr:`theme_cls` -attribute, with which you control the material properties of your application. +The main application class inherited from the :class:`~kivymd.app.MDApp` class +has the :attr:`~kivymd.app.MDApp.theme_cls` attribute, with which you control +the material properties of your application. Changing the theme colors ------------------------- @@ -36,142 +38,182 @@ the `colors `_ +.. note:: Your custom colors *must* use the names of the + `existing colors as defined in the palette `_ e.g. You can have `Blue` but you cannot have `NavyBlue`. -Add the custom theme to the MDApp as shown in the following snippet. +Add the custom theme to the :class:`~kivymd.app.MDApp` as shown in the +following snippet. -.. code-block:: python +.. tabs:: - from kivy.lang import Builder - from kivy.properties import ObjectProperty + .. tab:: Imperative python style with KV - from kivymd.app import MDApp - from kivymd.uix.floatlayout import MDFloatLayout - from kivymd.uix.tab import MDTabsBase - from kivymd.icon_definitions import md_icons + .. code-block:: python - colors = { - "Teal": { - "50": "e4f8f9", - "100": "bdedf0", - "200": "97e2e8", - "300": "79d5de", - "400": "6dcbd6", - "500": "6ac2cf", - "600": "63b2bc", - "700": "5b9ca3", - "800": "54888c", - "900": "486363", - "A100": "bdedf0", - "A200": "97e2e8", - "A400": "6dcbd6", - "A700": "5b9ca3", - }, - "Blue": { - "50": "e3f3f8", - "100": "b9e1ee", - "200": "91cee3", - "300": "72bad6", - "400": "62acce", - "500": "589fc6", - "600": "5191b8", - "700": "487fa5", - "800": "426f91", - "900": "35506d", - "A100": "b9e1ee", - "A200": "91cee3", - "A400": "62acce", - "A700": "487fa5", - }, - "Red": { - "50": "FFEBEE", - "100": "FFCDD2", - "200": "EF9A9A", - "300": "E57373", - "400": "EF5350", - "500": "F44336", - "600": "E53935", - "700": "D32F2F", - "800": "C62828", - "900": "B71C1C", - "A100": "FF8A80", - "A200": "FF5252", - "A400": "FF1744", - "A700": "D50000", - }, - "Light": { - "StatusBar": "E0E0E0", - "AppBar": "F5F5F5", - "Background": "FAFAFA", - "CardsDialogs": "FFFFFF", - "FlatButtonDown": "cccccc", - }, - "Dark": { - "StatusBar": "000000", - "AppBar": "212121", - "Background": "303030", - "CardsDialogs": "424242", - "FlatButtonDown": "999999", - } - } + from kivy.lang import Builder + from kivy.properties import ObjectProperty + + from kivymd.app import MDApp + from kivymd.uix.floatlayout import MDFloatLayout + from kivymd.uix.tab import MDTabsBase + from kivymd.icon_definitions import md_icons + + colors = { + "Teal": { + "200": "#212121", + "500": "#212121", + "700": "#212121", + }, + "Red": { + "200": "#C25554", + "500": "#C25554", + "700": "#C25554", + }, + "Light": { + "StatusBar": "E0E0E0", + "AppBar": "#202020", + "Background": "#2E3032", + "CardsDialogs": "#FFFFFF", + "FlatButtonDown": "#CCCCCC", + }, + } - KV = ''' - MDBoxLayout: - orientation: "vertical" + KV = ''' + MDBoxLayout: + orientation: "vertical" - MDTopAppBar: - title: "Example Tabs" + MDTopAppBar: + title: "Custom theme" - MDTabs: - id: tabs + MDTabs: + id: tabs - + - MDIconButton: - id: icon - icon: root.icon - user_font_size: "48sp" - pos_hint: {"center_x": .5, "center_y": .5} - ''' + MDIconButton: + id: icon + icon: root.icon + icon_size: "48sp" + theme_icon_color: "Custom" + icon_color: "white" + pos_hint: {"center_x": .5, "center_y": .5} + ''' - class Tab(MDFloatLayout, MDTabsBase): - '''Class implementing content for a tab.''' + class Tab(MDFloatLayout, MDTabsBase): + '''Class implementing content for a tab.''' - icon = ObjectProperty() + icon = ObjectProperty() - class Example(MDApp): - icons = list(md_icons.keys())[15:30] + class Example(MDApp): + icons = list(md_icons.keys())[15:30] - def build(self): - self.theme_cls.colors = colors - self.theme_cls.primary_palette = "Blue" - self.theme_cls.accent_palette = "Teal" - return Builder.load_string(KV) + def build(self): + self.theme_cls.colors = colors + self.theme_cls.primary_palette = "Teal" + self.theme_cls.accent_palette = "Red" + return Builder.load_string(KV) - def on_start(self): - for name_tab in self.icons: - tab = Tab(text="This is " + name_tab, icon=name_tab) - self.root.ids.tabs.add_widget(tab) + def on_start(self): + for name_tab in self.icons: + tab = Tab(title="This is " + name_tab, icon=name_tab) + self.root.ids.tabs.add_widget(tab) - Example().run() + Example().run() -This will change the theme colors to your custom defintion. In all other + .. tab:: Declarative python style + + .. code-block:: python + + from kivy.properties import ObjectProperty + + from kivymd.app import MDApp + from kivymd.uix.boxlayout import MDBoxLayout + from kivymd.uix.button import MDIconButton + from kivymd.uix.floatlayout import MDFloatLayout + from kivymd.uix.tab import MDTabsBase, MDTabs + from kivymd.icon_definitions import md_icons + from kivymd.uix.toolbar import MDTopAppBar + + colors = { + "Teal": { + "200": "#212121", + "500": "#212121", + "700": "#212121", + }, + "Red": { + "200": "#C25554", + "500": "#C25554", + "700": "#C25554", + }, + "Light": { + "StatusBar": "E0E0E0", + "AppBar": "#202020", + "Background": "#2E3032", + "CardsDialogs": "#FFFFFF", + "FlatButtonDown": "#CCCCCC", + }, + } + + + class Tab(MDFloatLayout, MDTabsBase): + '''Class implementing content for a tab.''' + + icon = ObjectProperty() + + + class Example(MDApp): + icons = list(md_icons.keys())[15:30] + + def build(self): + self.theme_cls.colors = colors + self.theme_cls.primary_palette = "Teal" + self.theme_cls.accent_palette = "Red" + + return ( + MDBoxLayout( + MDTopAppBar(title="Custom theme"), + MDTabs(id="tabs"), + orientation="vertical", + ) + ) + + def on_start(self): + for name_tab in self.icons: + self.root.ids.tabs.add_widget( + Tab( + MDIconButton( + icon=name_tab, + icon_size="48sp", + theme_icon_color="Custom", + icon_color="white", + pos_hint={"center_x": .5, "center_y": .5}, + ), + title="This is " + name_tab, + icon=name_tab, + ) + ) + + + Example().run() + +.. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/custom-color.png + :align: center + +This will change the theme colors to your custom definition. In all other respects, the theming stays as documented. .. warning:: Please note that the key ``'Red'`` is a required key for the - dictionary ``colors``. + dictionary :attr:`kivymd.color_definition.colors`. """ - +from kivy.animation import Animation from kivy.app import App -from kivy.atlas import Atlas from kivy.clock import Clock from kivy.core.window import Window from kivy.event import EventDispatcher @@ -181,13 +223,13 @@ from kivy.properties import ( BooleanProperty, ColorProperty, DictProperty, + NumericProperty, ObjectProperty, OptionProperty, StringProperty, ) from kivy.utils import get_color_from_hex -from kivymd import images_path from kivymd.color_definitions import colors, hue, palette from kivymd.font_definitions import theme_font_styles from kivymd.material_resources import DEVICE_IOS, DEVICE_TYPE @@ -207,29 +249,63 @@ class ThemeManager(EventDispatcher): To change the color scheme of an application: - .. code-block:: python + .. tabs:: - from kivymd.app import MDApp - from kivymd.uix.screen import MDScreen - from kivymd.uix.button import MDRectangleFlatButton + .. tab:: Imperative python style with KV + + .. code-block:: python + + from kivy.lang import Builder + + from kivymd.app import MDApp + + KV = ''' + MDScreen: + + MDRectangleFlatButton: + text: "Hello, World" + pos_hint: {"center_x": .5, "center_y": .5} + ''' - class MainApp(MDApp): - def build(self): - self.theme_cls.primary_palette = "Green" # "Purple", "Red" - screen = MDScreen() - screen.add_widget( - MDRectangleFlatButton( - text="Hello, World", - pos_hint={"center_x": 0.5, "center_y": 0.5}, - ) - ) - return screen + class Example(MDApp): + def build(self): + self.theme_cls.theme_style = "Dark" + self.theme_cls.primary_palette = "Red" # "Purple", "Red" + + return Builder.load_string(KV) - MainApp().run() + Example().run() + + .. tab:: Declarative python style + + .. code-block:: python + + from kivymd.app import MDApp + from kivymd.uix.button import MDRectangleFlatButton + from kivymd.uix.screen import MDScreen + + + class Example(MDApp): + def build(self): + self.theme_cls.theme_style = "Dark" + self.theme_cls.primary_palette = "Orange" # "Purple", "Red" + + return ( + MDScreen( + MDRectangleFlatButton( + text="Hello, World", + pos_hint={"center_x": 0.5, "center_y": 0.5}, + ) + ) + ) + + + Example().run() .. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/primary-palette.png + :align: center :attr:`primary_palette` is an :class:`~kivy.properties.OptionProperty` and defaults to `'Blue'`. @@ -244,36 +320,64 @@ class ThemeManager(EventDispatcher): To change the hue color scheme of an application: - .. code-block:: python + .. tabs:: - from kivymd.app import MDApp - from kivymd.uix.screen import MDScreen - from kivymd.uix.button import MDRectangleFlatButton + .. tab:: Imperative python style with KV + + .. code-block:: python + + from kivymd.app import MDApp + from kivymd.uix.screen import MDScreen + from kivymd.uix.button import MDRectangleFlatButton - class MainApp(MDApp): - def build(self): - self.theme_cls.primary_palette = "Green" # "Purple", "Red" - self.theme_cls.primary_hue = "200" # "500" - screen = MDScreen() - screen.add_widget( - MDRectangleFlatButton( - text="Hello, World", - pos_hint={"center_x": 0.5, "center_y": 0.5}, - ) - ) - return screen + class MainApp(MDApp): + def build(self): + self.theme_cls.primary_palette = "Orange" + self.theme_cls.primary_hue = "200" # "500" + screen = MDScreen() + screen.add_widget( + MDRectangleFlatButton( + text="Hello, World", + pos_hint={"center_x": 0.5, "center_y": 0.5}, + ) + ) + return screen - MainApp().run() + MainApp().run() - With a value of ``self.theme_cls.primary_hue = "500"``: + .. tab:: Declarative python style - .. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/primary-palette.png + .. code-block:: python - With a value of ``self.theme_cls.primary_hue = "200"``: + from kivymd.app import MDApp + from kivymd.uix.button import MDRectangleFlatButton + from kivymd.uix.screen import MDScreen - .. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/primary-hue.png + + class Example(MDApp): + def build(self): + self.theme_cls.primary_palette = "Orange" + self.theme_cls.theme_style = "Dark" + self.theme_cls.primary_hue = "200" # "500" + + return ( + MDScreen( + MDRectangleFlatButton( + text="Hello, World", + pos_hint={"center_x": 0.5, "center_y": 0.5}, + ) + ) + ) + + + Example().run() + + With a value of ``self.theme_cls.primary_hue = "200"`` and ``self.theme_cls.primary_hue = "500"``: + + .. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/primary_hue.png + :align: center :attr:`primary_hue` is an :class:`~kivy.properties.OptionProperty` and defaults to `'500'`. @@ -304,7 +408,7 @@ class ThemeManager(EventDispatcher): _get_primary_color, bind=("primary_palette", "primary_hue") ) """ - The color of the current application theme in ``rgba`` format. + The color of the current application theme. :attr:`primary_color` is an :class:`~kivy.properties.AliasProperty` that returns the value of the current application theme, property is readonly. @@ -319,42 +423,82 @@ class ThemeManager(EventDispatcher): _get_primary_light, bind=("primary_palette", "primary_light_hue") ) """ - Colors of the current application color theme in ``rgba`` format - (in lighter color). + Colors of the current application color theme (in lighter color). - .. code-block:: python + .. tabs:: - from kivy.lang import Builder + .. tab:: Declarative style with KV - from kivymd.app import MDApp + .. code-block:: python + + from kivy.lang import Builder + + from kivymd.app import MDApp - KV = ''' - MDScreen: + KV = ''' + MDScreen: - MDRaisedButton: - text: "primary_light" - pos_hint: {"center_x": 0.5, "center_y": 0.7} - md_bg_color: app.theme_cls.primary_light + MDRaisedButton: + text: "primary_light" + pos_hint: {"center_x": 0.5, "center_y": 0.7} + md_bg_color: app.theme_cls.primary_light - MDRaisedButton: - text: "primary_color" - pos_hint: {"center_x": 0.5, "center_y": 0.5} + MDRaisedButton: + text: "primary_color" + pos_hint: {"center_x": 0.5, "center_y": 0.5} - MDRaisedButton: - text: "primary_dark" - pos_hint: {"center_x": 0.5, "center_y": 0.3} - md_bg_color: app.theme_cls.primary_dark - ''' + MDRaisedButton: + text: "primary_dark" + pos_hint: {"center_x": 0.5, "center_y": 0.3} + md_bg_color: app.theme_cls.primary_dark + ''' - class MainApp(MDApp): - def build(self): - self.theme_cls.primary_palette = "Green" - return Builder.load_string(KV) + class MainApp(MDApp): + def build(self): + self.theme_cls.primary_palette = "Orange" + self.theme_cls.theme_style = "Dark" + return Builder.load_string(KV) - MainApp().run() + MainApp().run() + + .. tab:: Declarative python style + + .. code-block:: python + + from kivymd.app import MDApp + from kivymd.uix.button import MDRaisedButton + from kivymd.uix.screen import MDScreen + + + class Example(MDApp): + def build(self): + self.theme_cls.primary_palette = "Orange" + self.theme_cls.theme_style = "Dark" + + return ( + MDScreen( + MDRaisedButton( + text="Primary light", + pos_hint={"center_x": 0.5, "center_y": 0.7}, + md_bg_color=self.theme_cls.primary_light, + ), + MDRaisedButton( + text="Primary color", + pos_hint={"center_x": 0.5, "center_y": 0.5}, + ), + MDRaisedButton( + text="Primary dark", + pos_hint={"center_x": 0.5, "center_y": 0.3}, + md_bg_color=self.theme_cls.primary_dark, + ), + ) + ) + + + Example().run() .. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/primary-colors-light-dark.png :align: center @@ -373,8 +517,7 @@ class ThemeManager(EventDispatcher): _get_primary_dark, bind=("primary_palette", "primary_dark_hue") ) """ - Colors of the current application color theme - in ``rgba`` format (in darker color). + Colors of the current application color theme (in darker color). :attr:`primary_dark` is an :class:`~kivy.properties.AliasProperty` that returns the value of the current application theme (in darker color), @@ -384,12 +527,8 @@ class ThemeManager(EventDispatcher): accent_palette = OptionProperty("Amber", options=palette) """ The application color palette used for items such as the tab indicator - in the :attr:`MDTabsBar` class and so on... - - The image below shows the color schemes with the values - ``self.theme_cls.accent_palette = 'Blue'``, ``Red'`` and ``Yellow'``: - - .. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/accent-palette.png + in the :class:`~kivymd.uix.tab.MDTabsBar` class and so on. + See :attr:`kivymd.uix.tab.MDTabsBar.indicator_color` attribute. :attr:`accent_palette` is an :class:`~kivy.properties.OptionProperty` and defaults to `'Amber'`. @@ -397,8 +536,7 @@ class ThemeManager(EventDispatcher): accent_hue = OptionProperty("500", options=hue) """ - Similar to :attr:`primary_hue`, - but returns a value for :attr:`accent_palette`. + Similar to :attr:`primary_hue`, but returns a value for :attr:`accent_palette`. :attr:`accent_hue` is an :class:`~kivy.properties.OptionProperty` and defaults to `'500'`. @@ -429,12 +567,11 @@ class ThemeManager(EventDispatcher): _get_accent_color, bind=["accent_palette", "accent_hue"] ) """ - Similar to :attr:`primary_color`, but returns a value - for :attr:`accent_color`. + Similar to :attr:`primary_color`, but returns a value for :attr:`accent_color`. :attr:`accent_color` is an :class:`~kivy.properties.AliasProperty` that - returns the value in ``rgba`` format for :attr:`accent_color`, - property is readonly. + returns the value in ``rgba`` format for :attr:`accent_color`, property is + readonly. """ def _get_accent_light(self) -> list: @@ -446,12 +583,11 @@ class ThemeManager(EventDispatcher): _get_accent_light, bind=["accent_palette", "accent_light_hue"] ) """ - Similar to :attr:`primary_light`, but returns a value - for :attr:`accent_light`. + Similar to :attr:`primary_light`, but returns a value for :attr:`accent_light`. :attr:`accent_light` is an :class:`~kivy.properties.AliasProperty` that - returns the value in ``rgba`` format for :attr:`accent_light`, - property is readonly. + returns the value in ``rgba`` format for :attr:`accent_light`, property is + readonly. """ def _get_accent_dark(self) -> list: @@ -463,12 +599,11 @@ class ThemeManager(EventDispatcher): _get_accent_dark, bind=["accent_palette", "accent_dark_hue"] ) """ - Similar to :attr:`primary_dark`, but returns a value - for :attr:`accent_dark`. + Similar to :attr:`primary_dark`, but returns a value for :attr:`accent_dark`. :attr:`accent_dark` is an :class:`~kivy.properties.AliasProperty` that - returns the value in ``rgba`` format for :attr:`accent_dark`, - property is readonly. + returns the value in ``rgba`` format for :attr:`accent_dark`, property is + readonly. """ material_style = OptionProperty("M2", options=["M2", "M3"]) @@ -488,33 +623,211 @@ class ThemeManager(EventDispatcher): and defaults to `'M2'`. """ + theme_style_switch_animation = BooleanProperty(False) + """ + Animate app colors when switching app color scheme ('Dark/light'). + + .. versionadded:: 1.1.0 + + .. tabs:: + + .. tab:: Declarative KV style + + .. code-block:: python + + from kivy.lang import Builder + + from kivymd.app import MDApp + + KV = ''' + MDScreen: + + MDCard: + orientation: "vertical" + padding: 0, 0, 0 , "36dp" + size_hint: .5, .5 + pos_hint: {"center_x": .5, "center_y": .5} + elevation: 4 + shadow_radius: 6 + shadow_offset: 0, 2 + + MDLabel: + text: "Theme style - {}".format(app.theme_cls.theme_style) + halign: "center" + valign: "center" + bold: True + font_style: "H5" + + MDRaisedButton: + text: "Set theme" + on_release: app.switch_theme_style() + pos_hint: {"center_x": .5} + ''' + + + class Example(MDApp): + def build(self): + self.theme_cls.theme_style_switch_animation = True + self.theme_cls.theme_style = "Dark" + self.theme_cls.primary_palette = "Orange" + return Builder.load_string(KV) + + def switch_theme_style(self): + self.theme_cls.primary_palette = ( + "Orange" if self.theme_cls.primary_palette == "Red" else "Red" + ) + self.theme_cls.theme_style = ( + "Dark" if self.theme_cls.theme_style == "Light" else "Light" + ) + + + Example().run() + + .. tab:: Declarative python style + + .. code-block:: python + + from kivymd.app import MDApp + from kivymd.uix.button import MDRaisedButton + from kivymd.uix.card import MDCard + from kivymd.uix.label import MDLabel + from kivymd.uix.screen import MDScreen + + + class Example(MDApp): + def build(self): + self.theme_cls.theme_style_switch_animation = True + self.theme_cls.theme_style = "Dark" + self.theme_cls.primary_palette = "Orange" + return ( + MDScreen( + MDCard( + MDLabel( + id="label", + text="Theme style - {}".format(self.theme_cls.theme_style), + halign="center", + valign="center", + bold=True, + font_style="H5", + ), + MDRaisedButton( + text="Set theme", + on_release=self.switch_theme_style, + pos_hint={"center_x": 0.5}, + ), + id="card", + orientation="vertical", + padding=(0, 0, 0, "36dp"), + size_hint=(0.5, 0.5), + pos_hint={"center_x": 0.5, "center_y": 0.5}, + elevation=4, + shadow_radius=6, + shadow_offset=(0, 2), + ) + ) + ) + + def switch_theme_style(self, *args): + self.theme_cls.primary_palette = ( + "Orange" if self.theme_cls.primary_palette == "Red" else "Red" + ) + self.theme_cls.theme_style = ( + "Dark" if self.theme_cls.theme_style == "Light" else "Light" + ) + self.root.ids.card.ids.label.text = ( + "Theme style - {}".format(self.theme_cls.theme_style) + ) + + + Example().run() + + .. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/theme-style-switch-animation.gif + :align: center + + :attr:`theme_style_switch_animation` is an :class:`~kivy.properties.BooleanProperty` + and defaults to `False`. + """ + + theme_style_switch_animation_duration = NumericProperty(0.2) + """ + Duration of the animation of switching the color scheme of the application + ("Dark/light"). + + .. versionadded:: 1.1.0 + + .. code-block:: python + + class Example(MDApp): + def build(self): + self.theme_cls.theme_style_switch_animation = True + self.theme_cls.theme_style_switch_animation_duration = 0.8 + + .. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/theme-style-switch-animation-duration.gif + :align: center + + :attr:`theme_style_switch_animation_duration` is an :class:`~kivy.properties.NumericProperty` + and defaults to `0.2`. + """ + theme_style = OptionProperty("Light", options=["Light", "Dark"]) """ App theme style. - .. code-block:: python + .. tabs:: - from kivymd.app import MDApp - from kivymd.uix.screen import MDScreen - from kivymd.uix.button import MDRectangleFlatButton + .. tab:: Imperative python style + + .. code-block:: python + + from kivymd.app import MDApp + from kivymd.uix.screen import MDScreen + from kivymd.uix.button import MDRectangleFlatButton - class MainApp(MDApp): - def build(self): - self.theme_cls.theme_style = "Dark" # "Light" - screen = MDScreen() - screen.add_widget( - MDRectangleFlatButton( - text="Hello, World", - pos_hint={"center_x": 0.5, "center_y": 0.5}, - ) - ) - return screen + class MainApp(MDApp): + def build(self): + self.theme_cls.primary_palette = "Orange" + self.theme_cls.theme_style = "Dark" # "Light" + screen = MDScreen() + screen.add_widget( + MDRectangleFlatButton( + text="Hello, World", + pos_hint={"center_x": 0.5, "center_y": 0.5}, + ) + ) + return screen - MainApp().run() + MainApp().run() + + .. tab:: Declarative python style + + .. code-block:: python + + from kivymd.app import MDApp + from kivymd.uix.button import MDRectangleFlatButton + from kivymd.uix.screen import MDScreen + + + class Example(MDApp): + def build(self): + self.theme_cls.primary_palette = "Orange" + self.theme_cls.theme_style = "Dark" # "Light" + + return ( + MDScreen( + MDRectangleFlatButton( + text="Hello, World", + pos_hint={"center_x": 0.5, "center_y": 0.5}, + ), + ) + ) + + + Example().run() .. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/theme-style.png + :align: center :attr:`theme_style` is an :class:`~kivy.properties.OptionProperty` and defaults to `'Light'`. @@ -538,38 +851,76 @@ class ThemeManager(EventDispatcher): Similar to :attr:`bg_dark`, but the color values are a tone lower (darker) than :attr:`bg_dark`. - .. code-block:: python + .. tabs:: - KV = ''' - MDBoxLayout: + .. tab:: Declarative style with KV - MDBoxLayout: - md_bg_color: app.theme_cls.bg_light + .. code-block:: python - MDBoxLayout: - md_bg_color: app.theme_cls.bg_normal + from kivy.lang import Builder - MDBoxLayout: - md_bg_color: app.theme_cls.bg_dark + from kivymd.app import MDApp - MDBoxLayout: - md_bg_color: app.theme_cls.bg_darkest - ''' + KV = ''' + MDBoxLayout: - from kivy.lang import Builder + MDWidget: + md_bg_color: app.theme_cls.bg_light - from kivymd.app import MDApp + MDBoxLayout: + md_bg_color: app.theme_cls.bg_normal + + MDBoxLayout: + md_bg_color: app.theme_cls.bg_dark + + MDBoxLayout: + md_bg_color: app.theme_cls.bg_darkest + ''' - class MainApp(MDApp): - def build(self): - self.theme_cls.theme_style = "Dark" # "Light" - return Builder.load_string(KV) + class MainApp(MDApp): + def build(self): + self.theme_cls.theme_style = "Dark" # "Light" + return Builder.load_string(KV) - MainApp().run() + MainApp().run() + + .. tab:: Declarative python style + + .. code-block:: python + + from kivymd.app import MDApp + from kivymd.uix.boxlayout import MDBoxLayout + from kivymd.uix.widget import MDWidget + + + class Example(MDApp): + def build(self): + self.theme_cls.theme_style = "Dark" # "Light" + + return ( + MDBoxLayout( + MDWidget( + md_bg_color=self.theme_cls.bg_light, + ), + MDWidget( + md_bg_color=self.theme_cls.bg_normal, + ), + MDWidget( + md_bg_color=self.theme_cls.bg_dark, + ), + MDWidget( + md_bg_color=self.theme_cls.bg_darkest, + ), + ) + ) + + + Example().run() .. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/bg-normal-dark-darkest.png + :align: center :attr:`bg_darkest` is an :class:`~kivy.properties.AliasProperty` that returns the value in ``rgba`` format for :attr:`bg_darkest`, @@ -978,14 +1329,22 @@ class ThemeManager(EventDispatcher): ): self.set_clearcolor_by_theme_style(theme_style) - set_clearcolor = BooleanProperty(True) + _set_clearcolor = False def set_clearcolor_by_theme_style(self, theme_style): - if not self.set_clearcolor: - return - Window.clearcolor = get_color_from_hex( - self.colors[theme_style]["Background"] - ) + if self.theme_style_switch_animation and self._set_clearcolor: + Animation( + clearcolor=get_color_from_hex( + self.colors[theme_style]["Background"] + ), + d=self.theme_style_switch_animation_duration, + t="linear", + ).start(Window) + else: + Window.clearcolor = get_color_from_hex( + self.colors[theme_style]["Background"] + ) + self._set_clearcolor = True # Font name, size (sp), always caps, letter spacing (sp). font_styles = DictProperty( @@ -1009,45 +1368,93 @@ class ThemeManager(EventDispatcher): """ Data of default font styles. - Add custom font: + Add custom font + --------------- - .. code-block:: python + .. tabs:: - KV = ''' - MDScreen: + .. tab:: Declarative style with KV - MDLabel: - text: "JetBrainsMono" - halign: "center" - font_style: "JetBrainsMono" - ''' + .. code-block:: python - from kivy.core.text import LabelBase - from kivy.lang import Builder + from kivy.core.text import LabelBase + from kivy.lang import Builder - from kivymd.app import MDApp - from kivymd.font_definitions import theme_font_styles + from kivymd.app import MDApp + from kivymd.font_definitions import theme_font_styles + + KV = ''' + MDScreen: + + MDLabel: + text: "JetBrainsMono" + halign: "center" + font_style: "JetBrainsMono" + ''' - class MainApp(MDApp): - def build(self): - LabelBase.register( - name="JetBrainsMono", - fn_regular="JetBrainsMono-Regular.ttf") + class MainApp(MDApp): + def build(self): + self.theme_cls.theme_style = "Dark" - theme_font_styles.append('JetBrainsMono') - self.theme_cls.font_styles["JetBrainsMono"] = [ - "JetBrainsMono", - 16, - False, - 0.15, - ] - return Builder.load_string(KV) + LabelBase.register( + name="JetBrainsMono", + fn_regular="JetBrainsMono-Regular.ttf") + + theme_font_styles.append('JetBrainsMono') + self.theme_cls.font_styles["JetBrainsMono"] = [ + "JetBrainsMono", + 16, + False, + 0.15, + ] + return Builder.load_string(KV) - MainApp().run() + MainApp().run() + + .. tab:: Declarative python style + + .. code-block:: python + + from kivy.core.text import LabelBase + + from kivymd.app import MDApp + from kivymd.uix.screen import MDScreen + from kivymd.uix.label import MDLabel + from kivymd.font_definitions import theme_font_styles + + + class MainApp(MDApp): + def build(self): + self.theme_cls.theme_style = "Dark" + + LabelBase.register( + name="JetBrainsMono", + fn_regular="JetBrainsMono-Regular.ttf") + + theme_font_styles.append('JetBrainsMono') + self.theme_cls.font_styles["JetBrainsMono"] = [ + "JetBrainsMono", + 16, + False, + 0.15, + ] + return ( + MDScreen( + MDLabel( + text="JetBrainsMono", + halign="center", + font_style="JetBrainsMono", + ) + ) + ) + + + MainApp().run() .. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/font-styles.png + :align: center :attr:`font_styles` is an :class:`~kivy.properties.DictProperty`. """ @@ -1080,31 +1487,59 @@ class ThemeManager(EventDispatcher): Note that all values *must* be provided. If you only want to set one or two values use the appropriate method call for that. - .. code-block:: python + .. tabs:: - from kivymd.app import MDApp - from kivymd.uix.screen import MDScreen - from kivymd.uix.button import MDRectangleFlatButton + .. tab:: Imperative python style + + .. code-block:: python + + from kivymd.app import MDApp + from kivymd.uix.screen import MDScreen + from kivymd.uix.button import MDRectangleFlatButton + + class MainApp(MDApp): + def build(self): + self.theme_cls.set_colors( + "Blue", "600", "50", "800", "Teal", "600", "100", "800" + ) + screen = MDScreen() + screen.add_widget( + MDRectangleFlatButton( + text="Hello, World", + pos_hint={"center_x": 0.5, "center_y": 0.5}, + ) + ) + return screen - class MainApp(MDApp): - def build(self): - self.theme_cls.set_colors( - "Blue", "600", "50", "800", "Teal", "600", "100", "800" - ) - screen = MDScreen() - screen.add_widget( - MDRectangleFlatButton( - text="Hello, World", - pos_hint={"center_x": 0.5, "center_y": 0.5}, - ) - ) - return screen + MainApp().run() + + .. tab:: Declarative python style + + .. code-block:: python + + from kivymd.app import MDApp + from kivymd.uix.screen import MDScreen + from kivymd.uix.button import MDRectangleFlatButton + + class MainApp(MDApp): + def build(self): + self.theme_cls.set_colors( + "Blue", "600", "50", "800", "Teal", "600", "100", "800" + ) + return ( + MDScreen( + MDRectangleFlatButton( + text="Hello, World", + pos_hint={"center_x": 0.5, "center_y": 0.5}, + ) + ) + ) - MainApp().run() - + MainApp().run() """ + self.primary_palette = primary_palette self.primary_hue = primary_hue self.primary_light_hue = primary_light_hue @@ -1116,10 +1551,6 @@ class ThemeManager(EventDispatcher): def __init__(self, **kwargs): super().__init__(**kwargs) - self.rec_shadow = Atlas(f"{images_path}rec_shadow.atlas") - self.rec_st_shadow = Atlas(f"{images_path}rec_st_shadow.atlas") - self.quad_shadow = Atlas(f"{images_path}quad_shadow.atlas") - self.round_shadow = Atlas(f"{images_path}round_shadow.atlas") Clock.schedule_once(lambda x: self.on_theme_style(0, self.theme_style)) self._determine_device_orientation(None, Window.size) Window.bind(size=self._determine_device_orientation) @@ -1167,15 +1598,12 @@ class ThemableBehavior(EventDispatcher): MDSwitch: widget_style: "ios" - .. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/md-switch-ios.gif - :align: center - .. code-block:: kv MDSwitch: widget_style: "android" - .. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/md-switch-android.gif + .. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/switch-android-ios.png :align: center :attr:`widget_style` is an :class:`~kivy.properties.OptionProperty` @@ -1208,6 +1636,16 @@ class ThemableBehavior(EventDispatcher): """ def __init__(self, **kwargs): + self.unbind_properties = [ + "theme_style", + "material_style", + "device_orientation", + "primary_color", + "primary_palette", + "accent_palette", + "text_color", + ] + if self.theme_cls is not None: pass else: @@ -1228,3 +1666,24 @@ class ThemableBehavior(EventDispatcher): ) self.theme_cls = App.get_running_app().theme_cls super().__init__(**kwargs) + + # def dec_disabled(self, *args, **kwargs) -> None: + # callabacks = self.theme_cls.get_property_observers("theme_style") + + # for callaback in callabacks: + # try: + # if hasattr(callaback, "proxy") and hasattr( + # callaback.proxy, "theme_cls" + # ): + # for property_name in self.unbind_properties: + # self.theme_cls.unbind( + # **{ + # property_name: getattr( + # callaback.proxy, callaback.method_name + # ) + # } + # ) + # except ReferenceError: + # pass + + # super().dec_disabled(*args, **kwargs) diff --git a/sbapp/kivymd/tools/hotreload/app.py b/sbapp/kivymd/tools/hotreload/app.py index e036875..d3ef77f 100644 --- a/sbapp/kivymd/tools/hotreload/app.py +++ b/sbapp/kivymd/tools/hotreload/app.py @@ -298,8 +298,9 @@ class MDApp(BaseApp): from kivy.core.window import Window from kivy.utils import get_color_from_hex - Window.clearcolor = get_color_from_hex("#e50000") - scroll = Factory.ScrollView(scroll_y=0) + scroll = Factory.MDScrollView( + scroll_y=0, md_bg_color=get_color_from_hex("#e50000") + ) lbl = Factory.Label( text_size=(Window.width - 100, None), size_hint_y=None, diff --git a/sbapp/kivymd/tools/packaging/pyinstaller/hook-kivymd.py b/sbapp/kivymd/tools/packaging/pyinstaller/hook-kivymd.py index d2cb9a5..040d103 100644 --- a/sbapp/kivymd/tools/packaging/pyinstaller/hook-kivymd.py +++ b/sbapp/kivymd/tools/packaging/pyinstaller/hook-kivymd.py @@ -13,6 +13,18 @@ from pathlib import Path import kivymd datas = [ + # Add `.frag` files from the `kivymd/data/glsl/elevation` directory. + ( + str(Path(kivymd.glsl_path).joinpath("elevation")) + os.sep, + str( + Path("kivymd").joinpath( + str(Path(kivymd.glsl_path)).split(str(Path("kivymd")) + os.sep)[ + 1 + ] + + f"{os.sep}elevation" + ) + ), + ), # Add `.ttf` files from the `kivymd/fonts` directory. ( kivymd.fonts_path, diff --git a/sbapp/kivymd/tools/patterns/MVC/Controller/__init__.py b/sbapp/kivymd/tools/patterns/MVC/Controller/__init__.py deleted file mode 100644 index e69de29..0000000 diff --git a/sbapp/kivymd/tools/patterns/MVC/Controller/first_screen.py_tmp b/sbapp/kivymd/tools/patterns/MVC/Controller/first_screen.py_tmp deleted file mode 100644 index 1bcf0e8..0000000 --- a/sbapp/kivymd/tools/patterns/MVC/Controller/first_screen.py_tmp +++ /dev/null @@ -1,3 +0,0 @@ -%s - def get_view(self) -> %s: - return self.view diff --git a/sbapp/kivymd/tools/patterns/MVC/Makefile.tmp b/sbapp/kivymd/tools/patterns/MVC/Makefile.tmp deleted file mode 100644 index 4fd0715..0000000 --- a/sbapp/kivymd/tools/patterns/MVC/Makefile.tmp +++ /dev/null @@ -1,26 +0,0 @@ -# FILE TO FIND AND CREATE LOCALIZATION FILES FOR YOUR APPLICATION. \ -\ -In this file, you can specify in which files of your project to search for \ -localization strings. \ -These files should be listed in the below command: \ -\ -\ -xgettext -Lpython --output=messages.pot --from-code=utf-8 \ - path/to/file-1 \ - path/to/file-2 \ - ... - -.PHONY: po mo - -po: - xgettext -Lpython --output=messages.pot --from-code=utf-8 \ - View/%s/%s.kv \ - View/%s/%s.py - msgmerge --update --no-fuzzy-matching --backup=off data/locales/po/en.po messages.pot - msgmerge --update --no-fuzzy-matching --backup=off data/locales/po/ru.po messages.pot - -mo: - mkdir -p data/locales/en/LC_MESSAGES - mkdir -p data/locales/ru/LC_MESSAGES - msgfmt -c -o data/locales/en/LC_MESSAGES/%s.mo data/locales/po/en.po - msgfmt -c -o data/locales/ru/LC_MESSAGES/%s.mo data/locales/po/ru.po \ No newline at end of file diff --git a/sbapp/kivymd/tools/patterns/MVC/Model/base_model.py_tmp b/sbapp/kivymd/tools/patterns/MVC/Model/base_model.py_tmp deleted file mode 100644 index 554a4f4..0000000 --- a/sbapp/kivymd/tools/patterns/MVC/Model/base_model.py_tmp +++ /dev/null @@ -1,33 +0,0 @@ -# The model implements the observer pattern. This means that the class must -# support adding, removing, and alerting observers. In this case, the model is -# completely independent of controllers and views. It is important that all -# registered observers implement a specific method that will be called by the -# model when they are notified (in this case, it is the `model_is_changed` -# method). For this, observers must be descendants of an abstract class, -# inheriting which, the `model_is_changed` method must be overridden. - - -class BaseScreenModel: - """Implements a base class for model modules.""" - - _observers = [] - - def add_observer(self, observer) -> None: - self._observers.append(observer) - - def remove_observer(self, observer) -> None: - self._observers.remove(observer) - - def notify_observers(self, name_screen: str) -> None: - """ - Method that will be called by the observer when the model data changes. - - :param name_screen: - name of the view for which the method should be called - :meth:`model_is_changed`. - """ - - for observer in self._observers: - if observer.name == name_screen: - observer.model_is_changed() - break diff --git a/sbapp/kivymd/tools/patterns/MVC/Model/first_screen.py_tmp b/sbapp/kivymd/tools/patterns/MVC/Model/first_screen.py_tmp deleted file mode 100644 index 3aa8911..0000000 --- a/sbapp/kivymd/tools/patterns/MVC/Model/first_screen.py_tmp +++ /dev/null @@ -1 +0,0 @@ -%s diff --git a/sbapp/kivymd/tools/patterns/MVC/Utility/__init__.py b/sbapp/kivymd/tools/patterns/MVC/Utility/__init__.py deleted file mode 100644 index e69de29..0000000 diff --git a/sbapp/kivymd/tools/patterns/MVC/Utility/observer.py_tmp b/sbapp/kivymd/tools/patterns/MVC/Utility/observer.py_tmp deleted file mode 100644 index bbf1a9f..0000000 --- a/sbapp/kivymd/tools/patterns/MVC/Utility/observer.py_tmp +++ /dev/null @@ -1,16 +0,0 @@ -# Of course, "very flexible Python" allows you to do without an abstract -# superclass at all or use the clever exception `NotImplementedError`. In my -# opinion, this can negatively affect the architecture of the application. -# I would like to point out that using Kivy, one could use the on-signaling -# model. In this case, when the state changes, the model will send a signal -# that can be received by all attached observers. This approach seems less -# universal - you may want to use a different library in the future. - - -class Observer: - """Abstract superclass for all observers.""" - - def model_is_changed(self): - """ - The method that will be called on the observer when the model changes. - """ diff --git a/sbapp/kivymd/tools/patterns/MVC/View/FirstScreen/__init__.py b/sbapp/kivymd/tools/patterns/MVC/View/FirstScreen/__init__.py deleted file mode 100644 index e69de29..0000000 diff --git a/sbapp/kivymd/tools/patterns/MVC/View/FirstScreen/first_screen.kv b/sbapp/kivymd/tools/patterns/MVC/View/FirstScreen/first_screen.kv deleted file mode 100644 index 9d8c3b0..0000000 --- a/sbapp/kivymd/tools/patterns/MVC/View/FirstScreen/first_screen.kv +++ /dev/null @@ -1,72 +0,0 @@ -#:import images_path kivymd.images_path -#:import colors kivymd.color_definitions.colors -#:import get_color_from_hex kivy.utils.get_color_from_hex - - -<%s> - - FitImage: - source: - ( \ - f"{images_path}restdb-logo.png" \ - if root.model.database.name == "RestDB" else \ - f"{images_path}firebase-logo.png" \ - ) \ - if hasattr(root.model, "database") else \ - f"{images_path}transparent.png" - - MDBoxLayout: - orientation: "vertical" - - MDToolbar: - id: toolbar - title: "%s" - right_action_items: [["web", lambda x: %s]] - md_bg_color: - ( \ - get_color_from_hex(colors["Yellow"]["700"]) \ - if root.model.database.name == "Firebase" else \ - get_color_from_hex(colors["Blue"]["300"]) \ - ) \ - if hasattr(root.model, "database") else \ - app.theme_cls.primary_color - - MDFloatLayout: - - MDBoxLayout: - orientation: "vertical" - adaptive_height: True - size_hint_x: None - width: root.width - dp(72) - radius: 12 - padding: "12dp" - md_bg_color: 1, 1, 1, .5 - pos_hint: {"center_x": .5, "center_y": .5} - - MDLabel: - id: prev_label - text: %s - font_style: "H6" - adaptive_height: True - halign: "center" - color: 1, 1, 1, 1 - - MDBoxLayout: - orientation: "vertical" - adaptive_height: True - padding: "50dp" - spacing: "20dp" - - MDTextField: - hint_text: %s - on_text: root.controller.set_user_data("login", self.text) - - MDTextField: - hint_text: %s - on_text: root.controller.set_user_data("password", self.text) - - MDFillRoundFlatButton: - text: %s - on_release: root.controller.on_tap_button_login() - pos_hint: {"center_x": .5, "center_y": .1} - md_bg_color: toolbar.md_bg_color diff --git a/sbapp/kivymd/tools/patterns/MVC/View/FirstScreen/first_screen.py_tmp b/sbapp/kivymd/tools/patterns/MVC/View/FirstScreen/first_screen.py_tmp deleted file mode 100644 index f063c31..0000000 --- a/sbapp/kivymd/tools/patterns/MVC/View/FirstScreen/first_screen.py_tmp +++ /dev/null @@ -1,15 +0,0 @@ -%s -from View.base_screen import BaseScreenView - - -class %s(BaseScreenView): - """Implements the login start screen in the user application.""" -%s - def model_is_changed(self) -> None: - """ - Called whenever any change has occurred in the data model. - The view in this method tracks these changes and updates the UI - according to these changes. - """ - - %s \ No newline at end of file diff --git a/sbapp/kivymd/tools/patterns/MVC/View/base_screen.py_tmp b/sbapp/kivymd/tools/patterns/MVC/View/base_screen.py_tmp deleted file mode 100644 index 62a4242..0000000 --- a/sbapp/kivymd/tools/patterns/MVC/View/base_screen.py_tmp +++ /dev/null @@ -1,47 +0,0 @@ -from kivy.properties import ObjectProperty - -from kivymd.app import MDApp -from kivymd.theming import ThemableBehavior -from kivymd.uix.screen import MDScreen - -from Utility.observer import Observer - - -class BaseScreenView(ThemableBehavior, MDScreen, Observer): - """ - A base class that implements a visual representation of the model data - :class:`~Model.%s.%s`. - The view class must be inherited from this class. - """ - - controller = ObjectProperty() - """ - Controller object - :class:`~Controller.%s.%s`. - - :attr:`controller` is an :class:`~kivy.properties.ObjectProperty` - and defaults to `None`. - """ - - model = ObjectProperty() - """ - Model object - :class:`~Model.%s.%s`. - - :attr:`model` is an :class:`~kivy.properties.ObjectProperty` - and defaults to `None`. - """ - - manager_screens = ObjectProperty() - """ - Screen manager object - :class:`~kivy.uix.screenmanager.ScreenManager`. - - :attr:`manager_screens` is an :class:`~kivy.properties.ObjectProperty` - and defaults to `None`. - """ - - def __init__(self, **kw): - super().__init__(**kw) - # Often you need to get access to the application object from the view - # class. You can do this using this attribute. - self.app = MDApp.get_running_app() - # Adding a view class as observer. - self.model.add_observer(self) diff --git a/sbapp/kivymd/tools/patterns/MVC/View/screens.py_tmp b/sbapp/kivymd/tools/patterns/MVC/View/screens.py_tmp deleted file mode 100644 index f1ab8c3..0000000 --- a/sbapp/kivymd/tools/patterns/MVC/View/screens.py_tmp +++ /dev/null @@ -1,13 +0,0 @@ -# The screens dictionary contains the objects of the models and controllers -# of the screens of the application. - -from Model.%s import %s - -from Controller.%s import %s - -screens = { - %s: { - "model": %s, - "controller": %s, - }, -} diff --git a/sbapp/kivymd/tools/patterns/MVC/main.py_tmp b/sbapp/kivymd/tools/patterns/MVC/main.py_tmp deleted file mode 100644 index d697286..0000000 --- a/sbapp/kivymd/tools/patterns/MVC/main.py_tmp +++ /dev/null @@ -1,54 +0,0 @@ -""" -The entry point to the application. - -The application uses the MVC template. Adhering to the principles of clean -architecture means ensuring that your application is easy to test, maintain, -and modernize. - -You can read more about this template at the links below: - -https://github.com/HeaTTheatR/LoginAppMVC -https://en.wikipedia.org/wiki/Model–view–controller -""" - -from typing import NoReturn - -from kivy.uix.screenmanager import ScreenManager%s - -from kivymd.app import MDApp - -from View.screens import screens%s -%s - -class %s(MDApp):%s - def __init__(self, **kwargs): - super().__init__(**kwargs)%s - self.load_all_kv_files(self.directory) - # This is the screen manager that will contain all the screens of your - # application. - self.manager_screens = ScreenManager() - %s - def build(self) -> ScreenManager: - self.generate_application_screens() - return self.manager_screens - - def generate_application_screens(self) -> NoReturn: - """ - Creating and adding screens to the screen manager. - You should not change this cycle unnecessarily. He is self-sufficient. - - If you need to add any screen, open the `View.screens.py` module and - see how new screens are added according to the given application - architecture. - """ - - for i, name_screen in enumerate(screens.keys()): - model = screens[name_screen]["model"](%s) - controller = screens[name_screen]["controller"](model) - view = controller.get_view() - view.manager_screens = self.manager_screens - view.name = name_screen - self.manager_screens.add_widget(view) -%s%s - -%s().run() diff --git a/sbapp/kivymd/tools/patterns/add_view.py b/sbapp/kivymd/tools/patterns/add_view.py new file mode 100644 index 0000000..0187f01 --- /dev/null +++ b/sbapp/kivymd/tools/patterns/add_view.py @@ -0,0 +1,210 @@ +""" +The script creates a new View package +===================================== + +The script creates a new View package in an existing project with an MVC +template created using the create_project utility. + +.. versionadded:: 1.0.0 + +.. seealso:: + + `Utility create_project `_ + +.. rubric:: Use a clean architecture for your applications. + +To add a new view to an existing project that was created using the +`create_project` utility, use the following command:: + + kivymd.add_view \\ + name_pattern \\ + path_to_project \\ + name_view + +Example command:: + + kivymd.add_view \\ + MVC \\ + /Users/macbookair/Projects \\ + NewScreen + +You can also add new views with responsive behavior to an existing project:: + + kivymd.add_view \\ + MVC \\ + /Users/macbookair/Projects \\ + NewScreen \\ + --use_responsive yes + +For more information about adaptive design, +`see here `_. +""" + +__all__ = [ + "main", +] + +import os +import re + +from kivy import Logger + +from kivymd.tools.argument_parser import ArgumentParserWithHelp +from kivymd.tools.patterns.create_project import ( + chek_camel_case_name_project, + create_common_responsive_module, + create_controller, + create_model, + create_view, +) + +screens_data = """%s + +screens = {%s +}""" + +screns_comment = """# The screen's dictionary contains the objects of the models and controllers +# of the screens of the application. +""" + + +def main(): + """The function of adding a new view to the project.""" + + global screens_data + + parser = create_argument_parser() + args = parser.parse_args() + + # pattern_name isn't used currently, will be used if new patterns is added in future + pattern_name = args.pattern # noqa F841 + path_to_project = args.directory + name_view = args.name + use_responsive = args.use_responsive + + if not os.path.exists(path_to_project): + parser.error(f"Project <{path_to_project}> does not exist...") + + if name_view[-6:] != "Screen": + parser.error( + f"The name of the <{name_view}> screen should contain the word " + f"'Screen' at the end.\n" + "For example - '--name_screen MyFirstScreen ...'" + ) + + if name_view in os.listdir(os.path.join(path_to_project, "View")): + parser.error( + f"The <{name_view}> view also exists in the <{path_to_project}> project..." + ) + + # Create model. + name_database = ( + "yes" + if "database.py" in os.listdir(os.path.join(path_to_project, "Model")) + else "no" + ) + module_name = chek_camel_case_name_project(name_view) + if not module_name: + parser.error( + "The name of the screen should be written in camel case style. " + "\nFor example - 'MyFirstScreen'" + ) + module_name = "_".join([name.lower() for name in module_name]) + path_to_project = path_to_project + create_model(name_view, module_name, name_database, path_to_project) + + # Create controller. + # FIXME: This is not a very good solution in order to understand whether + # a project uses a hot reload or not. Because the string + # 'from kivymd.tools.hotreload.app import MDApp' in the project can just + # be commented out and the project does not actually use hot reload. + with open(os.path.join(path_to_project, "main.py")) as main_module: + if "from kivymd.tools.hotreload.app import MDApp" in main_module.read(): + use_hotreload = "yes" + else: + use_hotreload = "no" + create_controller( + name_view, module_name, use_hotreload, path_to_project + ) + # Create View. + if use_responsive == "no": + create_view(name_view, module_name, [], path_to_project) + else: + create_view(name_view, module_name, [name_view], path_to_project) + create_common_responsive_module([name_view], path_to_project) + # Create 'View.screens.py module'. + create_screens_data(name_view, module_name, path_to_project) + Logger.info( + f"KivyMD: The {name_view} view has been added to the project..." + ) + + +def create_screens_data( + name_view: str, module_name: str, path_to_project: str +) -> None: + with open( + os.path.join(path_to_project, "View", "screens.py") + ) as screen_module: + screen_module = screen_module.read() + imports = re.findall( + "from Model.*Model|from Controller.*Controller", screen_module + ) + screens = "" + path_to_view = os.path.join(path_to_project, "View") + + for name in os.listdir(path_to_view): + if os.path.isdir(os.path.join(path_to_view, name)): + res = re.findall("[A-Z][a-z]*", name) + if res and len(res) == 2 and res[-1] == "Screen": + screens += ( + "\n '%s': {" + "\n 'model': %s," + "\n 'controller': %s," + "\n }," + % ( + f"{res[0].lower()} {res[1].lower()}", + f'{"".join(res)}Model', + f'{"".join(res)}Controller', + ) + ) + + imports.append(f"from Model.{module_name} import {name_view}Model") + imports.append( + f"from Controller.{module_name} import {name_view}Controller" + ) + imports.insert(0, screns_comment) + screens = screens_data % ("\n".join(imports), screens) + + with open( + os.path.join(path_to_project, "View", "screens.py"), "w" + ) as screen_module: + screen_module.write(screens) + + +def create_argument_parser() -> ArgumentParserWithHelp: + parser = ArgumentParserWithHelp( + prog="create_project.py", + allow_abbrev=False, + ) + parser.add_argument( + "pattern", + help="the name of the pattern with which the project will be created.", + ) + parser.add_argument( + "directory", + help="the directory of the project to which you want to add a new view.", + ) + parser.add_argument( + "name", + help="the name of the view to add to an existing project.", + ) + parser.add_argument( + "--use_responsive", + default="no", + help="whether to create a view with responsive behavior.", + ) + return parser + + +if __name__ == "__main__": + main() diff --git a/sbapp/kivymd/tools/patterns/create_project.py b/sbapp/kivymd/tools/patterns/create_project.py index de59c1a..64487b0 100644 --- a/sbapp/kivymd/tools/patterns/create_project.py +++ b/sbapp/kivymd/tools/patterns/create_project.py @@ -25,7 +25,7 @@ Project creation Template command:: - python -m kivymd.tools.patterns.create_project \\ + kivymd.create_project \\ name_pattern \\ path_to_project \\ name_project \\ @@ -34,7 +34,7 @@ Template command:: Example command:: - python -m kivymd.tools.patterns.create_project \\ + kivymd.create_project \\ MVC \\ /Users/macbookair/Projects \\ MyMVCProject \\ @@ -61,7 +61,7 @@ Creating a project using a database Template command:: - python -m kivymd.tools.patterns.create_project \\ + kivymd.create_project \\ name_pattern \\ path_to_project \\ name_project \\ @@ -71,7 +71,7 @@ Template command:: Example command:: - python -m kivymd.tools.patterns.create_project \\ + kivymd.create_project \\ MVC \\ /Users/macbookair/Projects \\ MyMVCProject \\ @@ -99,18 +99,12 @@ the database restdb.io. `database_url` and `api_key` parameters on the test database (works only in read mode), so you should use your data for the database. -Preview of the basic MVC template using the RestDB database ------------------------------------------------------------ - -.. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/mvc-restbd-preview.png - :align: center - Create project with hot reload ------------------------------ Template command:: - python -m kivymd.tools.patterns.create_project \\ + kivymd.create_project \\ name_pattern \\ path_to_project \\ name_project \\ @@ -120,7 +114,7 @@ Template command:: Example command:: - python -m kivymd.tools.patterns.create_project \\ + kivymd.create_project \\ MVC \\ /Users/macbookair/Projects \\ MyMVCProject \\ @@ -133,8 +127,31 @@ information. Also, the necessary information is in other modules of the project in the form of comments. So do not forget to look at the source files of the created project. +Create project with responsive view +----------------------------------- + +When creating a project, you can specify which views should use responsive +behavior. To do this, specify the name of the view/views in the +`--use_responsive` argument: + +Template command:: + + kivymd.create_project \\ + name_pattern \\ + path_to_project \\ + name_project \\ + python_version \\ + kivy_version \\ + --name_screen FirstScreen SecondScreen ThirdScreen \\ + --use_responsive FirstScreen SecondScreen + +The `FirstScreen` and `SecondScreen` views will be created with an responsive +architecture. For more detailed information about using the adaptive view, see +the `MDResponsiveLayout `_ +widget. + Others command line arguments -====================== +============================= Required Arguments ------------------ @@ -161,6 +178,20 @@ Optional arguments - name_screen - the name of the class which be used when creating the project pattern +When you need to create an application template with multiple screens, +use multiple values separated by a space for the `name_screen` parameter, +for example, as shown below: + +Template command:: + + kivymd.create_project \\ + name_pattern \\ + path_to_project \\ + name_project \\ + python_version \\ + kivy_version \\ + --name_screen FirstScreen SecondScreen ThirdScreen + - name_database - provides a basic template for working with the 'firebase' library - or a complete implementation for working with a database 'restdb.io' @@ -168,14 +199,21 @@ Optional arguments - use_hotreload - creates a hot reload entry point to the application --use_localization +- use_localization - creates application localization files +- use_responsive + - the name/names of the views to be used by the responsive UI + .. warning:: On Windows, hot reloading of Python files may not work. But, for example, there is no such problem in macOS. If you fix this, please report it to the KivyMD community. """ +__all__ = [ + "main", +] + import os import re import shutil @@ -186,7 +224,42 @@ from kivy import Logger, platform from kivymd import path as kivymd_path from kivymd.tools.argument_parser import ArgumentParserWithHelp -_database_model = '''import multitasking +temp_basemodel = '''# The model implements the observer pattern. This means that the class must +# support adding, removing, and alerting observers. In this case, the model is +# completely independent of controllers and views. It is important that all +# registered observers implement a specific method that will be called by the +# model when they are notified (in this case, it is the `model_is_changed` +# method). For this, observers must be descendants of an abstract class, +# inheriting which, the `model_is_changed` method must be overridden. + + +class BaseScreenModel: + """Implements a base class for model modules.""" + + _observers = [] + + def add_observer(self, observer) -> None: + self._observers.append(observer) + + def remove_observer(self, observer) -> None: + self._observers.remove(observer) + + def notify_observers(self, name_screen: str) -> None: + """ + Method that will be called by the observer when the model data changes. + + :param name_screen: + name of the view for which the method should be called + :meth:`model_is_changed`. + """ + + for observer in self._observers: + if observer.name == name_screen: + observer.model_is_changed() + break +''' + +temp_database_model = '''import multitasking from Model.base_model import BaseScreenModel @@ -200,20 +273,16 @@ class {name_screen}Model(BaseScreenModel): """ def __init__(self, database): - self.database = database - # Dict: - # 'login': 'KivyMD' - # 'password': '1111' - self.user_data = dict() - self._data_validation_status = None + # Just an example of the data. Use your own values. + self._data = None @property - def data_validation_status(self): - return self._data_validation_status + def data(self): + return self._data - @data_validation_status.setter - def data_validation_status(self, value): - self._data_validation_status = value + @data.setter + def data(self, value): + self._data = value # We notify the View - # :class:`~View.{name_screen}.{module_name}.{name_screen}View` about the # changes that have occurred in the data model. @@ -221,37 +290,12 @@ class {name_screen}Model(BaseScreenModel): @multitasking.task def check_data(self): - """ - Get data from the database and compares this data with the data entered - by the user. This method is completely asynchronous. - It does not return any value. - """ + """Just an example of the method. Use your own code.""" - data = self.database.get_data_from_collection(self.database.USER_DATA) - data_validation_status = False - - if data: - if self.database.name == "RestDB": - data = data[0] - else: - data = list(data.values())[0] - if ( - data["login"] == self.user_data["login"] - and data["password"] == self.user_data["password"] - ): - data_validation_status = True - self.data_validation_status = data_validation_status - - def set_user_data(self, key: str, value: str) -> None: - """Sets a dictionary of data that the user enters.""" - - self.user_data[key] = value - - def reset_data_validation_status(self) -> None: - self.data_validation_status = None + self.data = ["example item"] ''' -_without_database_model = '''from Model.base_model import BaseScreenModel +temp_without_database_model = '''from Model.base_model import BaseScreenModel class {name_screen}Model(BaseScreenModel): @@ -260,41 +304,62 @@ class {name_screen}Model(BaseScreenModel): :class:`~View.{module_name}.{name_screen}.{name_screen}View` class. """''' -_database_controller = ''' -{import_module} +temp_screens_imports = """# The screens dictionary contains the objects of the models and controllers +# of the screens of the application. -class {name_screen}Controller: - """ - The `{name_screen}Controller` class represents a controller implementation. - Coordinates work of the view with the model. - The controller implements the strategy pattern. The controller connects to - the view to control its actions. - """ +""" - def __init__(self, model): - self.model = model # Model.{module_name}.{name_screen}Model object - self.view = {name_view}( - controller=self, model=self.model - ) +temp_code_responsive_view = '''from kivymd.uix.responsivelayout import MDResponsiveLayout - def set_user_data(self, key: str, value: str) -> None: - """Called every time the user enters text into the text fields.""" +from View.{name_screen}.components import ( + MobileScreenView, + TabletScreenView, + DesktopScreenView, +) +from View.base_screen import BaseScreenView - self.model.set_user_data(key, value) - def on_tap_button_login(self) -> None: - """Called when the `LOGIN` button is pressed.""" +class {name_screen}View(MDResponsiveLayout, BaseScreenView): + def __init__(self, **kw): + super().__init__(**kw) + self.mobile_view = MobileScreenView() + self.tablet_view = TabletScreenView() + self.desktop_view = DesktopScreenView() - self.view.show_dialog_wait() - self.model.check_data() - - def reset_data_validation_status(self, *args) -> None: - self.model.reset_data_validation_status() + def model_is_changed(self) -> None: + """ + Called whenever any change has occurred in the data model. + The view in this method tracks these changes and updates the UI + according to these changes. + """ ''' -_without_database_controller = ''' -{import_module} +temp_responsive_component_imports = """from .platforms.MobileScreen.mobile_screen import MobileScreenView +from .platforms.TabletScreen.tablet_screen import TabletScreenView +from .platforms.DesktopScreen.desktop_screen import DesktopScreenView +""" + +temp_responsive_platform_baseclass = """from kivymd.uix.screen import MDScreen + + +class {}View(MDScreen): + pass +""" + +temp_code_view = '''from View.base_screen import BaseScreenView + + +class {name_screen}View(BaseScreenView): + def model_is_changed(self) -> None: + """ + Called whenever any change has occurred in the data model. + The view in this method tracks these changes and updates the UI + according to these changes. + """ +''' + +temp_code_controller = '''{import_module} class {name_screen}Controller: @@ -309,71 +374,78 @@ class {name_screen}Controller: self.model = model # Model.{module_name}.{name_screen}Model self.view = {name_view}(controller=self, model=self.model) - def on_tap_button_login(self) -> None: - """Called when the `LOGIN` button is pressed.""" - - def set_user_data(self, key, value) -> None: - """Called every time the user enters text into the text fields.""" + def get_view(self) -> {get_view}: + return self.view ''' -_database_view_import = """from typing import Union +temp_base_screen = '''from kivy.properties import ObjectProperty -from kivy.clock import Clock +from kivymd.app import MDApp +from kivymd.theming import ThemableBehavior +from kivymd.uix.screen import MDScreen -from kivymd.uix.dialog import MDDialog -from kivymd.uix.snackbar import Snackbar -""" +from Utility.observer import Observer -_without_database_view_import = """ -""" -_database_view_methods = ''' def __init__(self, **kwargs): - super().__init__(**kwargs) - self.dialog = MDDialog() - self.dialog.bind(on_dismiss=self.controller.reset_data_validation_status) +class BaseScreenView(ThemableBehavior, MDScreen, Observer): + """ + A base class that implements a visual representation of the model data. + The view class must be inherited from this class. + """ - def show_dialog_wait(self) -> None: - """Displays a wait dialog while the model is processing data.""" + controller = ObjectProperty() + """ + Controller object - :class:`~Controller.controller_screen.ClassScreenControler`. - self.dialog.auto_dismiss = False - self.dialog.text = "Data validation..." - self.dialog.open() + :attr:`controller` is an :class:`~kivy.properties.ObjectProperty` + and defaults to `None`. + """ - def show_toast(self, interval: Union[int, float]) -> None: - Snackbar( - text="You have passed the verification successfully!", - snackbar_x="10dp", - snackbar_y="10dp", - size_hint_x=.8, - bg_color=self.theme_cls.primary_color, - ).open() + model = ObjectProperty() + """ + Model object - :class:`~Model.model_screen.ClassScreenModel`. + + :attr:`model` is an :class:`~kivy.properties.ObjectProperty` + and defaults to `None`. + """ + + manager_screens = ObjectProperty() + """ + Screen manager object - :class:`~kivymd.uix.screenmanager.MDScreenManager`. + + :attr:`manager_screens` is an :class:`~kivy.properties.ObjectProperty` + and defaults to `None`. + """ + + def __init__(self, **kw): + super().__init__(**kw) + # Often you need to get access to the application object from the view + # class. You can do this using this attribute. + self.app = MDApp.get_running_app() + # Adding a view class as observer. + self.model.add_observer(self) ''' -_database_view_model_is_changed_method = """if self.model.data_validation_status: - self.dialog.dismiss() - Clock.schedule_once(self.show_toast, 1) - if self.model.data_validation_status is False: - self.dialog.text = "Wrong data!" - self.dialog.auto_dismiss = True -""" +temp_utility = ''' +# Of course, "very flexible Python" allows you to do without an abstract +# superclass at all or use the clever exception `NotImplementedError`. In my +# opinion, this can negatively affect the architecture of the application. +# I would like to point out that using Kivy, one could use the on-signaling +# model. In this case, when the state changes, the model will send a signal +# that can be received by all attached observers. This approach seems less +# universal - you may want to use a different library in the future. -_firebase_requirements = """kivy==2.1.0 -kivymd==1.0.0 -multitasking -firebase -firebase-admin -python_jwt -gcloud -sseclient -pycryptodome==3.4.3 -requests_toolbelt -""" -_without_firebase_requirements = """kivy==2.1.0 -kivymd==1.0.0 -""" +class Observer: + """Abstract superclass for all observers.""" -_hot_reload_main = ''' + def model_is_changed(self): + """ + The method that will be called on the observer when the model changes. + """ +''' + +temp_hot_reload_main = ''' """ Script for managing hot reloading of the project. For more details see the documentation page - @@ -388,7 +460,6 @@ import importlib import os from kivy import Config -from kivy.uix.screenmanager import ScreenManager from PIL import ImageGrab @@ -399,26 +470,20 @@ resolution = ImageGrab.grab().size Config.set("graphics", "height", resolution[1]) Config.set("graphics", "width", "400") -from kivy.core.window import Window%s +from kivy.core.window import Window{} # Place the application window on the right side of the computer screen. Window.top = 0 Window.left = resolution[0] - Window.width from kivymd.tools.hotreload.app import MDApp -%s%s +from kivymd.uix.screenmanager import MDScreenManager +{}{} -class %s(MDApp): - KV_FILES = { - os.path.join( - os.getcwd(), - "View", - "%s", - "%s.kv", - ), - }%s +class {}(MDApp): + KV_DIRS = [os.path.join(os.getcwd(), "View")]{} - def build_app(self) -> ScreenManager: + def build_app(self) -> MDScreenManager: """ In this method, you don't need to change anything other than the application theme. @@ -426,14 +491,13 @@ class %s(MDApp): import View.screens - self.theme_cls.primary_palette = "Orange" - self.manager_screens = ScreenManager()%s%s + self.manager_screens = MDScreenManager(){}{} Window.bind(on_key_down=self.on_keyboard_down) importlib.reload(View.screens) screens = View.screens.screens for i, name_screen in enumerate(screens.keys()): - model = screens[name_screen]["model"](%s) + model = screens[name_screen]["model"]({}) controller = screens[name_screen]["controller"](model) view = controller.get_view() view.manager_screens = self.manager_screens @@ -451,20 +515,138 @@ class %s(MDApp): """ if "meta" in modifiers or "ctrl" in modifiers and text == "r": - self.rebuild()%s%s + self.rebuild(){}{} -%s().run() +{}().run() # After you finish the project, remove the above code and uncomment the below # code to test the application normally without hot reloading. ''' +temp_main = '''""" +The entry point to the application. + +The application uses the MVC template. Adhering to the principles of clean +architecture means ensuring that your application is easy to test, maintain, +and modernize. + +You can read more about this template at the links below: + +https://github.com/HeaTTheatR/LoginAppMVC +https://en.wikipedia.org/wiki/Model–view–controller +""" +{} +from kivymd.app import MDApp +from kivymd.uix.screenmanager import MDScreenManager + +from View.screens import screens{} +{} + +class {}(MDApp):{} + def __init__(self, **kwargs): + super().__init__(**kwargs){} + self.load_all_kv_files(self.directory) + # This is the screen manager that will contain all the screens of your + # application. + self.manager_screens = MDScreenManager() + {} + def build(self) -> MDScreenManager: + self.generate_application_screens() + return self.manager_screens + + def generate_application_screens(self) -> None: + """ + Creating and adding screens to the screen manager. + You should not change this cycle unnecessarily. He is self-sufficient. + + If you need to add any screen, open the `View.screens.py` module and + see how new screens are added according to the given application + architecture. + """ + + for i, name_screen in enumerate(screens.keys()): + model = screens[name_screen]["model"]({}) + controller = screens[name_screen]["controller"](model) + view = controller.get_view() + view.manager_screens = self.manager_screens + view.name = name_screen + self.manager_screens.add_widget(view) +{}{} + +{}().run() +''' + +temp_makefile = """# FILE TO FIND AND CREATE LOCALIZATION FILES FOR YOUR APPLICATION. \\ +\\ +In this file, you can specify in which files of your project to search for \\ +localization strings. \\ +These files should be listed in the below command: \\ +\\ +\\ +xgettext -Lpython --output=messages.pot --from-code=utf-8 \\ + path/to/file-1 \\ + path/to/file-2 \\ + ... + +.PHONY: po mo + +po: + xgettext -Lpython --output=messages.pot --from-code=utf-8 \\ +{} + msgmerge --update --no-fuzzy-matching --backup=off data/locales/po/en.po messages.pot + msgmerge --update --no-fuzzy-matching --backup=off data/locales/po/ru.po messages.pot + +mo: + mkdir -p data/locales/en/LC_MESSAGES + mkdir -p data/locales/ru/LC_MESSAGES + msgfmt -c -o data/locales/en/LC_MESSAGES/%s.mo data/locales/po/en.po + msgfmt -c -o data/locales/ru/LC_MESSAGES/%s.mo data/locales/po/ru.po +""" + +firebase_requirements = """kivy==2.1.0 +kivymd==1.0.0 +multitasking +firebase +firebase-admin +python_jwt +gcloud +sseclient +pycryptodome==3.4.3 +requests_toolbelt +""" + +without_firebase_requirements = """kivy==2.1.0 +kivymd==1.0.0 +""" + available_patterns = ["MVC"] available_databases = ["firebase", "restdb"] +path_to_project = "" +project_name = "" +use_localization = "" +name_database = "" +use_hotreload = "" +temp_makefile_files = "" +temp_screens_data = "" +kivy_version = "" +python_version = "" + def main(): + """Project creation function.""" + + global project_name + global use_localization + global name_database + global use_hotreload + global temp_makefile_files + global temp_screens_data + global path_to_project + global kivy_version + global python_version + parser = create_argument_parser() args = parser.parse_args() @@ -475,7 +657,7 @@ def main(): python_version = args.python_version if "3" not in python_version: parser.error("Python must be at least version 3") - name_screen = "".join(args.name_screen.split(" ")) + name_screen = args.name_screen path_to_project = os.path.join(project_directory, project_name) name_database = args.name_database if name_database != "no" and name_database not in available_databases: @@ -484,20 +666,16 @@ def main(): ) use_hotreload = args.use_hotreload use_localization = args.use_localization + use_responsive = args.use_responsive # Check arguments. - if name_screen[-6:] != "Screen": - parser.error( - "Name of the screen must contain the word 'Screen' at the end. " - "\nFor example - '... --name_screen MyFirstScreen'" - ) - module_name = chek_camel_case_name_project(name_screen) - if not module_name: - parser.error( - "The name of the screen should be written in camel case style. " - "\nFor example - 'MyFirstScreen'" - ) - module_name = "_".join([name.lower() for name in module_name]) + for name in name_screen: + if name[-6:] != "Screen": + parser.error( + f"The name of the {name} screen should contain the word " + f"'Screen' at the end.\n" + "For example - '--name_screen MyFirstScreen ...'" + ) if not os.path.exists( os.path.join(kivymd_path, "tools", "patterns", pattern_name) ): @@ -512,42 +690,53 @@ def main(): os.path.join(kivymd_path, "tools", "patterns", pattern_name), path_to_project, ) - create_main( - name_database, use_localization, path_to_project, project_name - ) - create_model(name_database, name_screen, module_name, path_to_project) - create_controller( - name_database, - use_hotreload, - name_screen, - module_name, - path_to_project, - ) - create_view( - name_database, - use_localization, - name_screen, - module_name, - path_to_project, - ) - create_requirements(name_database, path_to_project) + create_main() + + for name in name_screen: + module_name = chek_camel_case_name_project(name) + if not module_name: + parser.error( + "The name of the screen should be written in camel case style. " + "\nFor example - 'MyFirstScreen'" + ) + module_name = "_".join([name.lower() for name in module_name]) + + # Create models module. + create_model(name, module_name, name_database, path_to_project) + # Create controllers module. + create_controller(name, module_name, use_hotreload, path_to_project) + # Create screens data. + create_screens_data(name, module_name) + if use_localization == "yes": + # Create makefile data. + create_makefile_data(name, module_name) + # Create views. + create_view(name, module_name, use_responsive, path_to_project) + + # Create module `NameProject/View/NameScreen/components/common/__init__.py`. + create_common_responsive_module(use_responsive, path_to_project) + # Create module `NameProject/View/screens.py`. + create_module_screens() + # Create module `NameProject/Model/base_model.py`. + create_basemodel() + # Create module `NameProject/View/base_screen.py`. + create_module_basescreen() + # Create package `NameProject/Utility`. + create_package_utility() + # Create file `NameProject/Makefile`. + if use_localization == "yes": + # Create makefile data. + create_makefile() + + create_requirements() os.makedirs(os.path.join(path_to_project, "assets", "images")) os.mkdir(os.path.join(path_to_project, "assets", "fonts")) - rename_ext_py_tmp_to_py(path_to_project) - move_init(path_to_project, name_screen) if name_database != "no": - check_databases(name_database, path_to_project) + check_databases() if use_hotreload == "yes": - create_main_with_hotreload( - path_to_project, - project_name, - name_screen, - module_name, - name_database, - use_localization, - ) + create_main_with_hotreload() with open( os.path.join(path_to_project, "requirements.txt"), "a", @@ -557,36 +746,36 @@ def main(): if use_localization == "yes": Logger.info("KivyMD: Create localization files...") - create_makefile( - path_to_project, project_name, module_name, name_screen - ) - localization_po_file(path_to_project) - create_mofile(path_to_project) + os.chdir(path_to_project) + os.system("make po") + os.system("make mo") else: os.remove(os.path.join(path_to_project, "messages.pot")) os.remove(os.path.join(path_to_project, "libs", "translation.py")) shutil.rmtree(os.path.join(path_to_project, "data")) + Logger.info(f"KivyMD: Project '{path_to_project}' created") Logger.info( f"KivyMD: Create a virtual environment for '{path_to_project}' project..." ) - create_virtual_environment(python_version, path_to_project) + create_virtual_environment() Logger.info( f"KivyMD: Install requirements for '{path_to_project}' project..." ) - install_requirements(path_to_project, kivy_version, name_database) + install_requirements() + os.remove(os.path.join(path_to_project, "__init__.py")) + if name_database == "no": + os.remove( + os.path.join(path_to_project, "Model", "database_firebase.py") + ) + os.remove( + os.path.join(path_to_project, "Model", "database_restdb.py") + ) else: parser.error(f"The {path_to_project} project already exists") -def create_main_with_hotreload( - path_to_project: str, - project_name: str, - name_screen: str, - module_name: str, - name_database: str, - use_localization: str, -) -> None: +def create_main_with_hotreload() -> None: with open( os.path.join(path_to_project, "main.py"), encoding="utf-8" ) as main_file: @@ -596,12 +785,14 @@ def create_main_with_hotreload( with open( os.path.join(path_to_project, "main.py"), "w", encoding="utf-8" ) as main_file: - main_file.write(f"{_hot_reload_main}\n{main_code}") + main_file.write(f"{temp_hot_reload_main}\n{main_code}") - replace_in_file( - os.path.join(path_to_project, "main.py"), - ( - "\nfrom kivy.properties import StringProperty" + with open( + os.path.join(path_to_project, "main.py"), encoding="utf-8" + ) as main_file: + main_code = main_file.read() + main_code = main_code.format( + "\nfrom kivy.properties import StringProperty\n" if use_localization == "yes" else "", "\nfrom Model.database import DataBase" @@ -611,8 +802,6 @@ def create_main_with_hotreload( if use_localization == "yes" else "", project_name, - name_screen, - module_name, '\n lang = StringProperty("en")\n' if use_localization == "yes" else "", @@ -620,7 +809,7 @@ def create_main_with_hotreload( if name_database != "no" else "", "\n self.translation = Translation(\n" - ' self.lang, "%s", f"{self.directory}/data/locales"' + ' self.lang, "%s", os.path.join(self.directory, "data", "locales")' "\n )" % project_name if use_localization == "yes" else "", @@ -635,101 +824,98 @@ def create_main_with_hotreload( if use_localization == "yes" else "", project_name, - ), - ) + ) + with open( + os.path.join(path_to_project, "main.py"), "w", encoding="utf-8" + ) as main_module: + main_module.write(main_code) -def create_main( - name_database: str, - use_localization: str, - path_to_project: str, - project_name: str, -) -> None: - replace_in_file( - os.path.join(path_to_project, "main.py_tmp"), - ( - "\nfrom kivy.properties import StringProperty" - if use_localization == "yes" - else "", - "\nfrom libs.translation import Translation" - if use_localization == "yes" - else "", - "from Model.database import DataBase\n" - if name_database != "no" - else "", - project_name, - '\n lang = StringProperty("en")\n' - if use_localization == "yes" - else "", - "\n self.translation = Translation(\n" - ' self.lang, "%s", f"{self.directory}/data/locales"' - "\n )" % project_name - if use_localization == "yes" - else "", - "self.database = DataBase()\n" if name_database != "no" else "", - "self.database" if name_database != "no" else "", - "\n def on_lang(self, instance_app, lang_value: str) -> None:\n" - " self.translation.switch_lang(lang_value)\n" - if use_localization == "yes" - else "", - "\n def switch_lang(self) -> None:\n" - ' """Switch lang."""\n\n' - ' self.lang = "ru" if self.lang == "en" else "en"\n' - if use_localization == "yes" - else "", - project_name, - ), +def create_main() -> None: + main_code = temp_main.format( + "\nfrom kivy.properties import StringProperty\n" + if use_localization == "yes" + else "", + "\nfrom libs.translation import Translation" + if use_localization == "yes" + else "", + "from Model.database import DataBase\n" + if name_database != "no" + else "", + project_name, + '\n lang = StringProperty("en")\n' + if use_localization == "yes" + else "", + "\n self.translation = Translation(\n" + ' self.lang, "%s", os.path.join(self.directory, "data", "locales")' + "\n )" % project_name + if use_localization == "yes" + else "", + "self.database = DataBase()\n" if name_database != "no" else "", + "self.database" if name_database != "no" else "", + "\n def on_lang(self, instance_app, lang_value: str) -> None:\n" + " self.translation.switch_lang(lang_value)\n" + if use_localization == "yes" + else "", + "\n def switch_lang(self) -> None:\n" + ' """Switch lang."""\n\n' + ' self.lang = "ru" if self.lang == "en" else "en"\n' + if use_localization == "yes" + else "", + project_name, ) + with open( + os.path.join(path_to_project, "main.py"), "w", encoding="utf-8" + ) as main_module: + main_module.write(main_code) def create_model( - name_database: str, name_screen: str, module_name: str, path_to_project: str + name_screen: str, module_name: str, name_database: str, path_to_project: str ) -> None: if name_database != "no": - database_model = _database_model.format( + code_model = temp_database_model.format( name_screen=name_screen, module_name=module_name, notify_name_screen=f'"{" ".join(module_name.split("_"))}"', ) - replace_in_file( - os.path.join(path_to_project, "Model", "first_screen.py_tmp"), - (database_model), - ) else: - without_database_model = _without_database_model.format( + code_model = temp_without_database_model.format( module_name=module_name, name_screen=name_screen ) - replace_in_file( - os.path.join(path_to_project, "Model", "first_screen.py_tmp"), - (without_database_model), - ) - os.remove( - os.path.join(path_to_project, "Model", "database_firebase.py") - ) - os.remove(os.path.join(path_to_project, "Model", "database_restdb.py")) - os.rename( - os.path.join(path_to_project, "Model", "first_screen.py_tmp"), - os.path.join(path_to_project, "Model", f"{module_name}.py_tmp"), - ) + + model_module = os.path.join(path_to_project, "Model", module_name) + with open(f"{model_module}.py", "w", encoding="utf-8") as module: + module.write(code_model) + + +def create_basemodel() -> None: + with open( + os.path.join(path_to_project, "Model", "base_model.py"), + "w", + encoding="utf-8", + ) as module_basemodel: + module_basemodel.write(temp_basemodel) + + +def create_module_basescreen() -> None: + with open( + os.path.join(path_to_project, "View", "base_screen.py"), + "w", + encoding="utf-8", + ) as base_screen: + base_screen.write(temp_base_screen) def create_controller( - name_database: str, - use_hotreload: str, - name_screen: str, - module_name: str, - path_to_project: str, + name_screen: str, module_name: str, use_hotreload: str, path_to_project: str ) -> None: - if name_database != "no": - database_controller = _database_controller - else: - database_controller = _without_database_controller name_view = ( f"View.{name_screen}.{module_name}.{name_screen}View" if use_hotreload == "yes" else f"{name_screen}View" ) - database_controller = database_controller.format( + code_controller = temp_code_controller.format( name_screen=name_screen, module_name=module_name, import_module="" @@ -738,184 +924,200 @@ def create_controller( f"# We have to manually reload the view module in order to apply the\n" f"# changes made to the code on a subsequent hot reload.\n" f"# If you no longer need a hot reload, you can delete this instruction.\n" - f"importlib.reload(View.{name_screen}.{module_name})" + f"importlib.reload(View.{name_screen}.{module_name})\n\n" if use_hotreload == "yes" else f"\nfrom View.{name_screen}.{module_name} import {name_screen}View", name_view=name_view, + get_view=f"View.{name_screen}.{module_name}" + if use_hotreload == "yes" + else f"{name_screen}View", ) - replace_in_file( - os.path.join(path_to_project, "Controller", "first_screen.py_tmp"), - (database_controller, name_view), + + path_to_controller = os.path.join(path_to_project, "Controller") + if not os.path.exists(path_to_controller): + os.mkdir(path_to_controller) + controller_module = os.path.join(path_to_project, "Controller", module_name) + with open(f"{controller_module}.py", "w", encoding="utf-8") as module: + module.write(code_controller) + + +def create_makefile() -> None: + makefile = temp_makefile.format(temp_makefile_files[:-2]) + with open( + os.path.join(path_to_project, "Makefile"), "w", encoding="utf-8" + ) as make_file: + make_file.write(makefile) + + +def create_makefile_data(name_screen: str, module_name: str) -> None: + global temp_makefile_files + + temp_makefile_files += ( + f" View/{name_screen}/{module_name}.py \\\n" ) - os.rename( - os.path.join(path_to_project, "Controller", "first_screen.py_tmp"), - os.path.join(path_to_project, "Controller", f"{module_name}.py_tmp"), + temp_makefile_files += ( + f" View/{name_screen}/{module_name}.kv \\\n" ) -def create_view( - name_database: str, - use_localization: str, - name_screen: str, - module_name: str, - path_to_project: str, -) -> None: - replace_in_file( - os.path.join(path_to_project, "View", "screens.py_tmp"), - ( - module_name, - f"{name_screen}Model", - module_name, - f"{name_screen}Controller", +def create_screens_data(name_screen: str, module_name: str) -> None: + global temp_screens_imports + global temp_screens_data + + temp_screens_imports += ( + f"from Model.{module_name} import {name_screen}Model\n" + f"from Controller.{module_name} import {name_screen}Controller\n" + ) + temp_screens_data += ( + '\n %s: {\n "model": %s,' + '\n "controller": %s,\n },\n' + % ( f'"{" ".join(module_name.split("_"))}"', f"{name_screen}Model", f"{name_screen}Controller", - ), - ) - replace_in_file( - os.path.join(path_to_project, "View", "FirstScreen", "first_screen.kv"), - ( - f"{name_screen}View", - name_screen, - "app.switch_lang()" if use_localization == "yes" else "x", - 'app.translation._("To log in, enter your personal data:")' - if use_localization == "yes" - else '"To log in, enter your personal data:"', - 'app.translation._("Login")' - if use_localization == "yes" - else '"Login"', - 'app.translation._("Password")' - if use_localization == "yes" - else '"Password"', - 'app.translation._("LOGIN")' - if use_localization == "yes" - else '"LOGIN"', - ), - ) - replace_in_file( - os.path.join( - path_to_project, "View", "FirstScreen", "first_screen.py_tmp" - ), - ( - _database_view_import - if name_database != "no" - else _without_database_view_import, - f"{name_screen}View", - _database_view_methods if name_database != "no" else "", - _database_view_model_is_changed_method - if name_database != "no" - else "", - ), - ) - replace_in_file( - os.path.join(path_to_project, "View", "base_screen.py_tmp"), - ( - module_name, - f"{name_screen}Model", - module_name, - f"{name_screen}Controller", - module_name, - f"{name_screen}Model", - ), - ) - os.rename( - os.path.join(path_to_project, "View", "base_screen.py_tmp"), - os.path.join(path_to_project, "View", "base_screen.py"), - ) - os.rename( - os.path.join(path_to_project, "View", "FirstScreen", "first_screen.kv"), - os.path.join( - path_to_project, "View", "FirstScreen", f"{module_name}.kv" - ), - ) - os.rename( - os.path.join( - path_to_project, "View", "FirstScreen", "first_screen.py_tmp" - ), - os.path.join( - path_to_project, "View", "FirstScreen", f"{module_name}.py_tmp" - ), - ) - os.rename( - os.path.join(path_to_project, "View", "FirstScreen"), - os.path.join(path_to_project, "View", name_screen), + ) ) -def create_requirements(name_database: str, path_to_project: str) -> None: +def create_module_screens() -> None: + path_to_module_screens = os.path.join(path_to_project, "View", "screens.py") + with open(path_to_module_screens, "w", encoding="utf-8") as module_screens: + module_screens.write( + "%s\nscreens = {%s}" % (temp_screens_imports, temp_screens_data) + ) + + +def create_common_responsive_module( + use_responsive: list, path_to_project: str +) -> None: + for name_screen in use_responsive: + path_to_init_common = os.path.join( + path_to_project, "View", name_screen, "components", "common" + ) + os.makedirs(path_to_init_common) + with open( + os.path.join(path_to_init_common, "__init__.py"), + "w", + encoding="utf-8", + ) as init_common_components: + init_common_components.write( + "# This directory is for common responsive design components\n" + ) + + +def create_view( + name_screen: str, + module_name: str, + use_responsive: list, + path_to_project: str, +) -> None: + path_to_view = os.path.join(path_to_project, "View", name_screen) + path_to_components = os.path.join(path_to_view, "components") + view_module = os.path.join(path_to_view, module_name) + os.makedirs(path_to_view) + os.makedirs(path_to_components) + + with open( + os.path.join(path_to_view, "__init__.py"), "w", encoding="utf-8" + ) as init_module: + init_module.write("") + with open(f"{view_module}.py", "w", encoding="utf-8") as view_file: + view_file.write( + temp_code_view.format(name_screen=name_screen) + if name_screen not in use_responsive + else temp_code_responsive_view.format(name_screen=name_screen) + ) + + if name_screen in use_responsive: + for name_platform in ["DesktopScreen", "MobileScreen", "TabletScreen"]: + path_to_init_components = os.path.join( + path_to_project, + "View", + name_screen, + "components", + "__init__.py", + ) + path_to_platforms = os.path.join( + path_to_project, "View", name_screen, "components", "platforms" + ) + path_to_platform = os.path.join(path_to_platforms, name_platform) + path_to_platform_components = os.path.join( + path_to_platform, "components" + ) + os.makedirs(path_to_platform_components) + shutil.copy( + os.path.join(path_to_view, "__init__.py"), + path_to_platform_components, + ) + shutil.copy( + os.path.join(path_to_view, "__init__.py"), path_to_platforms + ) + + name_platform_module = ( + f'{name_platform.split("Screen")[0].lower()}_screen' + ) + with open( + os.path.join(path_to_platform, f"{name_platform_module}.kv"), + "w", + encoding="utf-8", + ) as platform_rule: + platform_rule.write(f"<{name_platform}View>\n") + with open( + os.path.join(path_to_platform, f"{name_platform_module}.py"), + "w", + encoding="utf-8", + ) as platform_baseclass: + platform_baseclass.write( + temp_responsive_platform_baseclass.format(name_platform) + ) + + with open( + path_to_init_components, "w", encoding="utf-8" + ) as init_components: + init_components.write(temp_responsive_component_imports) + + with open(f"{view_module}.kv", "w", encoding="utf-8") as view_file: + view_file.write(f"<{name_screen}View>\n") + + if name_screen not in use_responsive: + shutil.copy( + os.path.join(path_to_view, "__init__.py"), path_to_components + ) + + +def create_package_utility() -> None: + path_to_utility = os.path.join(path_to_project, "Utility") + os.mkdir(path_to_utility) + + with open( + os.path.join(path_to_utility, "__init__.py"), "w", encoding="utf-8" + ) as init_module: + init_module.write("") + with open( + os.path.join(path_to_utility, "observer.py"), "w", encoding="utf-8" + ) as observer: + observer.write(temp_utility) + + +def create_requirements() -> None: with open( os.path.join(path_to_project, "requirements.txt"), "w", encoding="utf-8" ) as requirements: requirements.write( - _firebase_requirements - if name_database != "no" - else _without_firebase_requirements + firebase_requirements + if name_database == "firebase" + else without_firebase_requirements ) -def create_makefile( - path_to_project: str, project_name: str, module_name: str, name_screen: str -) -> None: - path_to_makefile_tmp = os.path.join(path_to_project, "Makefile.tmp") - replace_in_file( - path_to_makefile_tmp, - ( - name_screen, - module_name, - name_screen, - module_name, - project_name, - project_name, - ), - ) - os.rename(path_to_makefile_tmp, os.path.splitext(path_to_makefile_tmp)[0]) - os.chdir(path_to_project) - os.system("make po") - - -def create_mofile(path_to_project: str) -> None: - os.chdir(path_to_project) - os.system("make mo") - - -def create_virtual_environment( - python_version: str, path_to_project: str -) -> None: +def create_virtual_environment() -> None: os.system(f"{python_version} -m pip install virtualenv") os.system( f"virtualenv -p {python_version} {os.path.join(path_to_project, 'venv')}" ) -def localization_po_file(path_to_project: str) -> None: - path_to_file_po = os.path.join( - path_to_project, "data", "locales", "po", "ru.po" - ) - with open(path_to_file_po, "rt", encoding="utf-8") as file_po: - file_po_content = ( - file_po.read() - .replace( - 'msgid "To log in, enter your personal data:"\nmsgstr ""', - 'msgid "To log in, enter your personal data:"\nmsgstr "Для входа введите свои личные данные"', - ) - .replace( - 'msgid "Login"\nmsgstr ""', 'msgid "Login"\nmsgstr "Логин"' - ) - .replace( - 'msgid "Password"\nmsgstr ""', - 'msgid "Password"\nmsgstr "Пароль"', - ) - .replace( - 'msgid "LOGIN"\nmsgstr ""', 'msgid "LOGIN"\nmsgstr "ЛОГИН"' - ) - ) - with open(path_to_file_po, "wt", encoding="utf-8") as file_po: - file_po.write(file_po_content) - - -def install_requirements( - path_to_project: str, kivy_version: str, name_database: str -) -> None: +def install_requirements() -> None: python = os.path.join(path_to_project, "venv", "bin", "python3") if kivy_version == "master": if platform == "macosx": @@ -952,39 +1154,7 @@ def install_requirements( ) -def rename_ext_py_tmp_to_py(path_to_project: str) -> None: - for path_to_dir, dirs, files in os.walk(path_to_project): - for name_file in files: - if os.path.splitext(name_file)[1] == ".py_tmp": - os.rename( - os.path.join(path_to_dir, name_file), - os.path.join( - path_to_dir, f"{os.path.splitext(name_file)[0]}.py" - ), - ) - - -def move_init(path_to_project: str, name_screen: str) -> None: - path_to_init_file = __file__.replace("create_project", "__init__") - for name_dir in ("Controller", "Model", "Utility", "View"): - shutil.copy( - path_to_init_file, - os.path.join(path_to_project, name_dir, "__init__.py"), - ) - shutil.copy( - path_to_init_file, - os.path.join(path_to_project, "View", name_screen, "__init__.py"), - ) - path_to_components = os.path.join( - path_to_project, "View", name_screen, "components" - ) - os.mkdir(path_to_components) - shutil.copy( - path_to_init_file, os.path.join(path_to_components, "__init__.py") - ) - - -def check_databases(name_database: str, path_to_project: str) -> None: +def check_databases() -> None: databases = {"firebase": "restdb", "restdb": "firebase"} os.remove( os.path.join( @@ -1004,13 +1174,6 @@ def chek_camel_case_name_project(name_project) -> Union[bool, list]: return result -def replace_in_file(path_to_file: str, args) -> None: - with open(path_to_file, "rt", encoding="utf-8") as file_content: - new_file_content = file_content.read() % (args) - with open(path_to_file, "wt", encoding="utf-8") as original_file: - original_file.write(new_file_content) - - def create_argument_parser() -> ArgumentParserWithHelp: parser = ArgumentParserWithHelp( prog="create_project.py", @@ -1040,8 +1203,17 @@ def create_argument_parser() -> ArgumentParserWithHelp: ) parser.add_argument( "--name_screen", - default="MainScreen", - help="the name of the class wich be used when creating the project pattern.", + nargs="*", + type=str, + default=["MainScreen"], + help="the name/names of the class which be used when creating the project pattern.", + ) + parser.add_argument( + "--use_responsive", + nargs="*", + type=str, + default=[], + help="the name/names of the views to be used by the responsive UI.", ) parser.add_argument( "--name_database", diff --git a/sbapp/kivymd/tools/release/git_commands.py b/sbapp/kivymd/tools/release/git_commands.py index e194765..fc82f44 100644 --- a/sbapp/kivymd/tools/release/git_commands.py +++ b/sbapp/kivymd/tools/release/git_commands.py @@ -1,4 +1,4 @@ -# Copyright (c) 2019-2021 Artem Bulgakov +# Copyright (c) 2019-2022 Artem Bulgakov # # This file is distributed under the terms of the same license, # as the Kivy framework. diff --git a/sbapp/kivymd/tools/release/make_release.py b/sbapp/kivymd/tools/release/make_release.py index fbb616a..f321a2d 100644 --- a/sbapp/kivymd/tools/release/make_release.py +++ b/sbapp/kivymd/tools/release/make_release.py @@ -1,4 +1,4 @@ -# Copyright (c) 2019-2021 Artem Bulgakov +# Copyright (c) 2019-2022 Artem Bulgakov # # This file is distributed under the terms of the same license, # as the Kivy framework. diff --git a/sbapp/kivymd/tools/release/update_icons.py b/sbapp/kivymd/tools/release/update_icons.py index 2e24148..13fd43d 100644 --- a/sbapp/kivymd/tools/release/update_icons.py +++ b/sbapp/kivymd/tools/release/update_icons.py @@ -1,4 +1,4 @@ -# Copyright (c) 2019-2021 Artem Bulgakov +# Copyright (c) 2019-2022 Artem Bulgakov # # This file is distributed under the terms of the same license, # as the Kivy framework. diff --git a/sbapp/kivymd/uix/anchorlayout.py b/sbapp/kivymd/uix/anchorlayout.py index c50fa0c..4e2be4a 100644 --- a/sbapp/kivymd/uix/anchorlayout.py +++ b/sbapp/kivymd/uix/anchorlayout.py @@ -10,7 +10,7 @@ with some widget properties. For example: AnchorLayout ------------ -.. code-block:: +.. code-block:: kv AnchorLayout: canvas: @@ -20,65 +20,13 @@ AnchorLayout pos: self.pos size: self.size -AnchorLayout ------------- - -.. code-block:: - - MDBoxLayout: - md_bg_color: app.theme_cls.primary_color - -Available options are: ----------------------- - -- adaptive_height_ -- adaptive_width_ -- adaptive_size_ - -.. adaptive_height: -adaptive_height ---------------- - -.. code-block:: kv - - adaptive_height: True - -Equivalent - -.. code-block:: kv - - size_hint_y: None - height: self.minimum_height - -.. adaptive_width: -adaptive_width +MDAnchorLayout -------------- .. code-block:: kv - adaptive_width: True - -Equivalent - -.. code-block:: kv - - size_hint_x: None - height: self.minimum_width - -.. adaptive_size: -adaptive_size -------------- - -.. code-block:: kv - - adaptive_size: True - -Equivalent - -.. code-block:: kv - - size_hint: None, None - size: self.minimum_size + MDAnchorLayout: + md_bg_color: app.theme_cls.primary_color """ __all__ = ("MDAnchorLayout",) @@ -86,7 +34,11 @@ __all__ = ("MDAnchorLayout",) from kivy.uix.anchorlayout import AnchorLayout from kivymd.uix import MDAdaptiveWidget +from kivymd.uix.behaviors import DeclarativeBehavior -class MDAnchorLayout(AnchorLayout, MDAdaptiveWidget): - pass +class MDAnchorLayout(DeclarativeBehavior, AnchorLayout, MDAdaptiveWidget): + """ + Anchor layout class. For more information, see in the + :class:`~kivy.uix.anchorlayout.AnchorLayout` class documentation. + """ diff --git a/sbapp/kivymd/uix/backdrop/backdrop.kv b/sbapp/kivymd/uix/backdrop/backdrop.kv index 372c754..2f56d5b 100644 --- a/sbapp/kivymd/uix/backdrop/backdrop.kv +++ b/sbapp/kivymd/uix/backdrop/backdrop.kv @@ -35,8 +35,8 @@ if not root.front_layer_color \ else root.front_layer_color radius: - [root.radius_left, root.radius_left, - root.radius_right, root.radius_right] + [root.radius_left, root.radius_right, + 0, 0] OneLineListItem: id: header_button diff --git a/sbapp/kivymd/uix/backdrop/backdrop.py b/sbapp/kivymd/uix/backdrop/backdrop.py index 77a3028..604ab13 100644 --- a/sbapp/kivymd/uix/backdrop/backdrop.py +++ b/sbapp/kivymd/uix/backdrop/backdrop.py @@ -31,84 +31,147 @@ Usage Example ------- -.. code-block:: python +.. tabs:: - from kivy.lang import Builder + .. tab:: Declarative KV styles - from kivymd.uix.screen import MDScreen - from kivymd.app import MDApp + .. code-block:: python - # Your layouts. - Builder.load_string( - ''' - #:import Window kivy.core.window.Window - #:import IconLeftWidget kivymd.uix.list.IconLeftWidget + from kivy.lang import Builder + + from kivymd.uix.screen import MDScreen + from kivymd.app import MDApp + + # Your layouts. + Builder.load_string( + ''' + #:import os os + #:import Window kivy.core.window.Window + #:import IconLeftWidget kivymd.uix.list.IconLeftWidget + #:import images_path kivymd.images_path - - icon: "android" + + icon: "android" - IconLeftWidget: - icon: root.icon + IconLeftWidget: + icon: root.icon - - backdrop: None - text: "Lower the front layer" - secondary_text: " by 50 %" - icon: "transfer-down" - on_press: root.backdrop.open(-Window.height / 2) - pos_hint: {"top": 1} - _no_ripple_effect: True + + backdrop: None + text: "Lower the front layer" + secondary_text: " by 50 %" + icon: "transfer-down" + on_press: root.backdrop.open(-Window.height / 2) + pos_hint: {"top": 1} + _no_ripple_effect: True - - size_hint: .8, .8 - source: "data/logo/kivy-icon-512.png" - pos_hint: {"center_x": .5, "center_y": .6} - ''' - ) + + size_hint: .8, .8 + source: os.path.join(images_path, "logo", "kivymd-icon-512.png") + pos_hint: {"center_x": .5, "center_y": .6} + ''' + ) - # Usage example of MDBackdrop. - Builder.load_string( - ''' - + # Usage example of MDBackdrop. + Builder.load_string( + ''' + - MDBackdrop: - id: backdrop - left_action_items: [['menu', lambda x: self.open()]] - title: "Example Backdrop" - radius_left: "25dp" - radius_right: "0dp" - header_text: "Menu:" + MDBackdrop: + id: backdrop + left_action_items: [['menu', lambda x: self.open()]] + title: "Example Backdrop" + radius_left: "25dp" + radius_right: "0dp" + header_text: "Menu:" - MDBackdropBackLayer: - MyBackdropBackLayer: - id: backlayer + MDBackdropBackLayer: + MyBackdropBackLayer: + id: backlayer - MDBackdropFrontLayer: - MyBackdropFrontLayer: - backdrop: backdrop - ''' - ) + MDBackdropFrontLayer: + MyBackdropFrontLayer: + backdrop: backdrop + ''' + ) - class ExampleBackdrop(MDScreen): - pass + class ExampleBackdrop(MDScreen): + pass - class TestBackdrop(MDApp): - def __init__(self, **kwargs): - super().__init__(**kwargs) - - def build(self): - return ExampleBackdrop() + class Example(MDApp): + def build(self): + self.theme_cls.theme_style = "Dark" + self.theme_cls.primary_palette = "Orange" + return ExampleBackdrop() - TestBackdrop().run() + Example().run() + + .. tab:: Declarative python styles + + .. code-block:: python + + import os + + from kivy.core.window import Window + from kivy.uix.image import Image + + from kivymd import images_path + from kivymd.uix.backdrop import MDBackdrop + from kivymd.uix.backdrop.backdrop import ( + MDBackdropBackLayer, MDBackdropFrontLayer + ) + from kivymd.uix.list import TwoLineAvatarListItem, IconLeftWidget + from kivymd.uix.screen import MDScreen + from kivymd.app import MDApp + + + class Example(MDApp): + def build(self): + self.theme_cls.theme_style = "Dark" + self.theme_cls.primary_palette = "Orange" + + return ( + MDScreen( + MDBackdrop( + MDBackdropBackLayer( + Image( + size_hint=(0.8, 0.8), + source=os.path.join(images_path, "logo", "kivymd-icon-512.png"), + pos_hint={"center_x": 0.5, "center_y": 0.6}, + ) + ), + MDBackdropFrontLayer( + TwoLineAvatarListItem( + IconLeftWidget(icon="transfer-down"), + text="Lower the front layer", + secondary_text=" by 50 %", + on_press=self.backdrop_open_by_50_percent, + pos_hint={"top": 1}, + _no_ripple_effect=True, + ), + ), + id="backdrop", + title="Example Backdrop", + radius_left="25dp", + radius_right="0dp", + header_text="Menu:", + ) + ) + ) + + def backdrop_open_by_50_percent(self, *args): + self.root.ids.backdrop.open(-Window.height / 2) + + + Example().run() .. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/backdrop.gif - :width: 280 px :align: center .. Note:: `See full example `_ @@ -139,7 +202,7 @@ from kivy.uix.boxlayout import BoxLayout from kivymd import uix_path from kivymd.theming import ThemableBehavior -from kivymd.uix.behaviors import FakeRectangularElevationBehavior +from kivymd.uix.boxlayout import MDBoxLayout from kivymd.uix.card import MDCard from kivymd.uix.floatlayout import MDFloatLayout from kivymd.uix.toolbar.toolbar import ActionTopAppBarButton, MDTopAppBar @@ -151,7 +214,7 @@ with open( Builder.load_string(kv_file.read()) -class MDBackdrop(ThemableBehavior, MDFloatLayout): +class MDBackdrop(MDFloatLayout, ThemableBehavior): """ :Events: :attr:`on_open` @@ -167,6 +230,9 @@ class MDBackdrop(ThemableBehavior, MDFloatLayout): .. versionadded:: 1.0.0 + .. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/backdrop-anchor-title.png + :align: center + :attr:`anchor_title` is an :class:`~kivy.properties.OptionProperty` and defaults to `'left'`. """ @@ -175,6 +241,9 @@ class MDBackdrop(ThemableBehavior, MDFloatLayout): """ Padding for contents of the front layer. + .. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/backdrop-padding.png + :align: center + :attr:`padding` is an :class:`~kivy.properties.ListProperty` and defaults to `[0, 0, 0, 0]`. """ @@ -210,6 +279,9 @@ class MDBackdrop(ThemableBehavior, MDFloatLayout): """ Background color of back layer. + .. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/backdrop-back-layer-color.png + :align: center + :attr:`back_layer_color` is an :class:`~kivy.properties.ColorProperty` and defaults to `None`. """ @@ -218,6 +290,9 @@ class MDBackdrop(ThemableBehavior, MDFloatLayout): """ Background color of front layer. + .. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/backdrop-front-layer-color.png + :align: center + :attr:`front_layer_color` is an :class:`~kivy.properties.ColorProperty` and defaults to `None`. """ @@ -227,6 +302,9 @@ class MDBackdrop(ThemableBehavior, MDFloatLayout): The value of the rounding radius of the upper left corner of the front layer. + .. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/backdrop-radius-left.png + :align: center + :attr:`radius_left` is an :class:`~kivy.properties.NumericProperty` and defaults to `16dp`. """ @@ -244,6 +322,9 @@ class MDBackdrop(ThemableBehavior, MDFloatLayout): """ Whether to use a header above the contents of the front layer. + .. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/backdrop-header.png + :align: center + :attr:`header` is an :class:`~kivy.properties.BooleanProperty` and defaults to `True`. """ @@ -252,6 +333,9 @@ class MDBackdrop(ThemableBehavior, MDFloatLayout): """ Text of header. + .. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/backdrop-header-text.png + :align: center + :attr:`header_text` is an :class:`~kivy.properties.StringProperty` and defaults to `'Header'`. """ @@ -261,6 +345,9 @@ class MDBackdrop(ThemableBehavior, MDFloatLayout): The name of the icon that will be installed on the toolbar on the left when opening the front layer. + .. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/backdrop-close-icon.png + :align: center + :attr:`close_icon` is an :class:`~kivy.properties.StringProperty` and defaults to `'close'`. """ @@ -310,8 +397,8 @@ class MDBackdrop(ThemableBehavior, MDFloatLayout): _open_icon = "" _front_layer_open = False - def __init__(self, **kwargs): - super().__init__(**kwargs) + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) self.register_event_type("on_open") self.register_event_type("on_close") Clock.schedule_once( @@ -332,8 +419,11 @@ class MDBackdrop(ThemableBehavior, MDFloatLayout): self._open_icon = self.left_action_items[0][0] def on_header(self, instance_backdrop, value: bool) -> None: - if not value: - self.ids._front_layer.remove_widget(self.ids.header_button) + def on_header(*args): + if not value: + self.ids._front_layer.remove_widget(self.ids.header_button) + + Clock.schedule_once(on_header) def open(self, open_up_to: int = 0) -> None: """ @@ -425,11 +515,11 @@ class MDBackdropToolbar(MDTopAppBar): """Implements a toolbar for back content.""" -class MDBackdropFrontLayer(BoxLayout): +class MDBackdropFrontLayer(MDBoxLayout): """Container for front content.""" -class MDBackdropBackLayer(BoxLayout): +class MDBackdropBackLayer(MDBoxLayout): """Container for back content.""" @@ -437,5 +527,5 @@ class _BackLayer(BoxLayout): pass -class _FrontLayer(MDCard, FakeRectangularElevationBehavior): +class _FrontLayer(MDCard): pass diff --git a/sbapp/kivymd/uix/banner/banner.py b/sbapp/kivymd/uix/banner/banner.py index 7feb5c9..fb87221 100644 --- a/sbapp/kivymd/uix/banner/banner.py +++ b/sbapp/kivymd/uix/banner/banner.py @@ -35,7 +35,7 @@ Usage MDTopAppBar: id: toolbar title: "Example Banners" - elevation: 10 + elevation: 4 pos_hint: {'top': 1} MDBoxLayout: @@ -157,7 +157,6 @@ from kivy.properties import ( from kivy.uix.widget import Widget from kivymd import uix_path -from kivymd.uix.behaviors import FakeRectangularElevationBehavior from kivymd.uix.boxlayout import MDBoxLayout from kivymd.uix.button import MDFlatButton from kivymd.uix.card import MDCard @@ -177,7 +176,7 @@ with open( Builder.load_string(kv_file.read()) -class MDBanner(MDCard, FakeRectangularElevationBehavior): +class MDBanner(MDCard): vertical_pad = NumericProperty(dp(68)) """ Indent the banner at the top of the screen. diff --git a/sbapp/kivymd/uix/behaviors/__init__.py b/sbapp/kivymd/uix/behaviors/__init__.py index 639acff..e89ed10 100755 --- a/sbapp/kivymd/uix/behaviors/__init__.py +++ b/sbapp/kivymd/uix/behaviors/__init__.py @@ -5,22 +5,26 @@ Behaviors Modules and classes implementing various behaviors for buttons etc. """ -# flake8: NOQA -from .hover_behavior import HoverBehavior # isort:skip from .backgroundcolor_behavior import ( BackgroundColorBehavior, SpecificBackgroundColorBehavior, ) + +# flake8: NOQA +from .declarative_behavior import DeclarativeBehavior from .elevation import ( CircularElevationBehavior, CommonElevationBehavior, FakeCircularElevationBehavior, FakeRectangularElevationBehavior, - ObservableShadow, RectangularElevationBehavior, RoundedRectangularElevationBehavior, ) -from .focus_behavior import FocusBehavior from .magic_behavior import MagicBehavior from .ripple_behavior import CircularRippleBehavior, RectangularRippleBehavior +from .rotate_behavior import RotateBehavior +from .scale_behavior import ScaleBehavior +from .stencil_behavior import StencilBehavior from .touch_behavior import TouchBehavior + +from .hover_behavior import HoverBehavior # isort:skip diff --git a/sbapp/kivymd/uix/behaviors/backgroundcolor_behavior.py b/sbapp/kivymd/uix/behaviors/backgroundcolor_behavior.py index d96fd60..4c4649b 100755 --- a/sbapp/kivymd/uix/behaviors/backgroundcolor_behavior.py +++ b/sbapp/kivymd/uix/behaviors/backgroundcolor_behavior.py @@ -7,8 +7,9 @@ Behaviors/Background Color __all__ = ("BackgroundColorBehavior", "SpecificBackgroundColorBehavior") -from typing import List +from typing import List, Union +from kivy.animation import Animation from kivy.lang import Builder from kivy.properties import ( ColorProperty, @@ -24,8 +25,6 @@ from kivy.utils import get_color_from_hex from kivymd.color_definitions import hue, palette, text_colors from kivymd.theming import ThemeManager -from .elevation import CommonElevationBehavior - Builder.load_string( """ #:import RelativeLayout kivy.uix.relativelayout.RelativeLayout @@ -38,7 +37,7 @@ Builder.load_string( angle: self.angle origin: self._background_origin Color: - rgba: self.md_bg_color + rgba: self._md_bg_color RoundedRectangle: group: "Background_instruction" size: self.size @@ -67,7 +66,7 @@ Builder.load_string( ) -class BackgroundColorBehavior(CommonElevationBehavior): +class BackgroundColorBehavior: background = StringProperty() """ Background image path. @@ -153,15 +152,26 @@ class BackgroundColorBehavior(CommonElevationBehavior): _background_x = NumericProperty(0) _background_y = NumericProperty(0) - _background_origin = ReferenceListProperty( - _background_x, - _background_y, - ) + _background_origin = ReferenceListProperty(_background_x, _background_y) + _md_bg_color = ColorProperty([0, 0, 0, 0]) def __init__(self, **kwarg): super().__init__(**kwarg) self.bind(pos=self.update_background_origin) + def on_md_bg_color(self, instance_md_widget, color: Union[list, str]): + if ( + hasattr(self, "theme_cls") + and self.theme_cls.theme_style_switch_animation + ): + Animation( + _md_bg_color=color, + d=self.theme_cls.theme_style_switch_animation_duration, + t="linear", + ).start(self) + else: + self._md_bg_color = color + def update_background_origin( self, instance_md_widget, pos: List[float] ) -> None: @@ -206,12 +216,14 @@ class SpecificBackgroundColorBehavior(BackgroundColorBehavior): super().__init__(**kwargs) if hasattr(self, "theme_cls"): self.theme_cls.bind( - primary_palette=self._update_specific_text_color + primary_palette=self._update_specific_text_color, + accent_palette=self._update_specific_text_color, + theme_style=self._update_specific_text_color, ) - self.theme_cls.bind(accent_palette=self._update_specific_text_color) - self.theme_cls.bind(theme_style=self._update_specific_text_color) - self.bind(background_hue=self._update_specific_text_color) - self.bind(background_palette=self._update_specific_text_color) + self.bind( + background_hue=self._update_specific_text_color, + background_palette=self._update_specific_text_color, + ) self._update_specific_text_color(None, None) def _update_specific_text_color( @@ -234,5 +246,17 @@ class SpecificBackgroundColorBehavior(BackgroundColorBehavior): secondary_color[3] = 0.54 else: secondary_color[3] = 0.7 - self.specific_text_color = color - self.specific_secondary_text_color = secondary_color + + if ( + hasattr(self, "theme_cls") + and self.theme_cls.theme_style_switch_animation + ): + Animation( + specific_text_color=color, + specific_secondary_text_color=secondary_color, + d=self.theme_cls.theme_style_switch_animation_duration, + t="linear", + ).start(self) + else: + self.specific_text_color = color + self.specific_secondary_text_color = secondary_color diff --git a/sbapp/kivymd/uix/behaviors/declarative_behavior.py b/sbapp/kivymd/uix/behaviors/declarative_behavior.py new file mode 100644 index 0000000..0fb5eb7 --- /dev/null +++ b/sbapp/kivymd/uix/behaviors/declarative_behavior.py @@ -0,0 +1,317 @@ +""" +Behaviors/Declarative +===================== + +.. versionadded:: 1.0.0 + +.. raw:: html + +
+ +
+ +As you already know, the Kivy framework provides the best/simplest/modern +UI creation tool that allows you to separate the logic of your application +from the description of the properties of widgets/GUI components. +This tool is named `KV Language `_. + +But in addition to creating a user interface using the KV Language Kivy allows +you to create user interface elements directly in the Python code. +And if you've ever created a user interface in Python code, you know how ugly +it looks. Even in the simplest user interface design, which was created using +Python code it is impossible to trace the widget tree, because in Python code +you build the user interface in an imperative style. + +Imperative style +---------------- + +.. code-block:: python + + from kivymd.app import MDApp + from kivymd.uix.bottomnavigation import MDBottomNavigation, MDBottomNavigationItem + from kivymd.uix.label import MDLabel + from kivymd.uix.screen import MDScreen + + + class Example(MDApp): + def build(self): + screen = MDScreen() + bottom_navigation = MDBottomNavigation( + panel_color="#eeeaea", + selected_color_background="#97ecf8", + text_color_active="white", + ) + + data = { + "screen 1": {"text": "Mail", "icon": "gmail"}, + "screen 2": {"text": "Discord", "icon": "discord"}, + "screen 3": {"text": "LinkedIN", "icon": "linkedin"}, + } + for key in data.keys(): + text = data[key]["text"] + navigation_item = MDBottomNavigationItem( + name=key, text=text, icon=data[key]["icon"] + ) + navigation_item.add_widget(MDLabel(text=text, halign="center")) + bottom_navigation.add_widget(navigation_item) + + screen.add_widget(bottom_navigation) + return screen + + + Example().run() + +.. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/bottom-navigation-styles-programming.png + :align: center + +Take a look at the above code example. This is a very simple UI. But looking +at this code, you will not be able to figure the widget tree and understand +which UI this code implements. This is named imperative programming style, +which is used in Kivy. + +Now let's see how the same code is implemented using the KV language, +which uses a declarative style of describing widget properties. + +Declarative style with KV language +---------------------------------- + +.. code-block:: python + + from kivy.lang import Builder + + from kivymd.app import MDApp + + + class Test(MDApp): + def build(self): + return Builder.load_string( + ''' + MDScreen: + + MDBottomNavigation: + panel_color: "#eeeaea" + selected_color_background: "#97ecf8" + text_color_active: "white" + + MDBottomNavigationItem: + name: "screen 1" + text: "Mail" + icon: "gmail" + + MDLabel: + text: "Mail" + halign: "center" + + MDBottomNavigationItem: + name: "screen 2" + text: "Discord" + icon: "discord" + + MDLabel: + text: "Discord" + halign: "center" + + MDBottomNavigationItem: + name: "screen 3" + text: "LinkedIN" + icon: "linkedin" + + MDLabel: + text: "LinkedIN" + halign: "center" + ''' + ) + + + Test().run() + +.. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/bottom-navigation-styles-programming.png + :align: center + +Looking at this code, we can now clearly see the widget tree and their properties. +We can quickly navigate through the components of the screen and quickly +change/add new properties/widgets. This is named declarative UI creation style. + +But now the KivyMD library allows you to write Python code in a declarative style. +Just as it is implemented in Flutter/Jetpack Compose/SwiftUI. + +Declarative style with Python code +---------------------------------- + +.. code-block:: python + + from kivymd.app import MDApp + from kivymd.uix.bottomnavigation import MDBottomNavigation, MDBottomNavigationItem + from kivymd.uix.label import MDLabel + from kivymd.uix.screen import MDScreen + + + class Example(MDApp): + def build(self): + return ( + MDScreen( + MDBottomNavigation( + MDBottomNavigationItem( + MDLabel( + text="Mail", + halign="center", + ), + name="screen 1", + text="Mail", + icon="gmail", + ), + MDBottomNavigationItem( + MDLabel( + text="Discord", + halign="center", + ), + name="screen 2", + text="Discord", + icon="discord", + ), + MDBottomNavigationItem( + MDLabel( + text="LinkedIN", + halign="center", + ), + name="screen 3", + text="LinkedIN", + icon="linkedin", + ), + panel_color="#eeeaea", + selected_color_background="#97ecf8", + text_color_active="white", + ) + ) + ) + + + Example().run() + +.. note:: The KivyMD library does not support creating Kivy widgets in Python + code in a declarative style. + +But you can still use the declarative style of creating Kivy widgets in Python code. +To do this, you need to create a new class that will be inherited from the Kivy +widget and the :class:`~DeclarativeBehavior` class: + +.. code-block:: python + + from kivy.uix.boxlayout import BoxLayout + from kivy.uix.button import Button + + from kivymd.app import MDApp + from kivymd.uix.behaviors import DeclarativeBehavior + + + class DeclarativeStyleBoxLayout(DeclarativeBehavior, BoxLayout): + pass + + + class Example(MDApp): + def build(self): + return ( + DeclarativeStyleBoxLayout( + Button(), + Button(), + orientation="vertical", + ) + ) + + + Example().run() + +Get objects by identifiers +-------------------------- + +In the declarative style in Python code, the ids parameter of the specified +widget will return only the id of the child widget/container, ignoring other ids. +Therefore, to get objects by identifiers in declarative style in Python code, +you must specify all the container ids in which the widget is nested until you +get to the desired id: + +.. code-block:: + + from kivymd.app import MDApp + from kivymd.uix.boxlayout import MDBoxLayout + from kivymd.uix.button import MDRaisedButton + from kivymd.uix.floatlayout import MDFloatLayout + + + class Example(MDApp): + def build(self): + return ( + MDBoxLayout( + MDFloatLayout( + MDRaisedButton( + id="button_1", + text="Button 1", + pos_hint={"center_x": 0.5, "center_y": 0.5}, + ), + id="box_container_1", + ), + MDBoxLayout( + MDFloatLayout( + MDRaisedButton( + id="button_2", + text="Button 2", + pos_hint={"center_x": 0.5, "center_y": 0.5}, + ), + id="float_container", + ), + id="box_container_2", + ) + ) + ) + + def on_start(self): + # { + # 'box_container_1': , + # 'box_container_2': + # } + print(self.root.ids) + + # + print(self.root.ids.box_container_2.ids.float_container.ids.button_2) + + + Example().run() + +Yes, this is not a very good solution, but I think it will be fixed soon. + +.. warning:: Declarative programming style in Python code in the KivyMD library + is an experimental feature. Therefore, if you receive errors, do not hesitate + to create new issue in the KivyMD repository. +""" + +from kivy.properties import StringProperty +from kivy.uix.widget import Widget + + +class DeclarativeBehavior: + """ + Implements the creation and addition of child widgets as declarative + programming style. + """ + + id = StringProperty() + """ + Widget ID. + + :attr:`id` is an :class:`~kivy.properties.StringProperty` + and defaults to `''`. + """ + + def __init__(self, *args, **kwargs): + super().__init__(**kwargs) + + for child in args: + if issubclass(child.__class__, Widget): + self.add_widget(child) + if hasattr(child, "id") and child.id: + self.ids[child.id] = child diff --git a/sbapp/kivymd/uix/behaviors/elevation.py b/sbapp/kivymd/uix/behaviors/elevation.py index 584c82b..5b23dba 100755 --- a/sbapp/kivymd/uix/behaviors/elevation.py +++ b/sbapp/kivymd/uix/behaviors/elevation.py @@ -11,419 +11,12 @@ Behaviors/Elevation .. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/elevation-previous.png :align: center -There are 5 classes in KivyMD that can simulate shadow: +To create an elevation effect, use the :class:`~CommonElevationBehavior` class. +For example, let's create a button with a rectangular elevation effect: - #. :class:`~FakeRectangularElevationBehavior` - #. :class:`~FakeCircularElevationBehavior` +.. tabs:: - #. :class:`~RectangularElevationBehavior` - #. :class:`~CircularElevationBehavior` - #. :class:`~RoundedRectangularElevationBehavior` - -By default, KivyMD widgets use the elevation behavior implemented in classes -:class:`~FakeRectangularElevationBehavior` and :class:`~FakeCircularElevationBehavior` -for cast shadows. These classes use the old method of rendering shadows and it -doesn't look very aesthetically pleasing. Shadows are harsh, no softness: - -The :class:`~RectangularElevationBehavior`, :class:`~CircularElevationBehavior`, -:class:`~RoundedRectangularElevationBehavior` classes use the new shadow -rendering algorithm, based on textures creation using the `Pillow` library. -It looks very aesthetically pleasing and beautiful. - -.. warning:: Remember that :class:`~RectangularElevationBehavior`, - :class:`~CircularElevationBehavior`, :class:`~RoundedRectangularElevationBehavior` - classes require a lot of resources from the device on which your application will run, - so you should not use these classes on mobile devices. - -.. code-block:: python - - from kivy.lang import Builder - from kivy.uix.widget import Widget - - from kivymd.app import MDApp - from kivymd.uix.card import MDCard - from kivymd.uix.behaviors import RectangularElevationBehavior - from kivymd.uix.boxlayout import MDBoxLayout - - KV = ''' - - adaptive_size: True - orientation: "vertical" - spacing: "36dp" - - - - size_hint: None, None - size: 100, 100 - md_bg_color: 0, 0, 1, 1 - elevation: 36 - pos_hint: {'center_x': .5} - - - MDFloatLayout: - - MDBoxLayout: - adaptive_size: True - pos_hint: {'center_x': .5, 'center_y': .5} - spacing: "56dp" - - Box: - - MDLabel: - text: "Deprecated shadow rendering" - adaptive_size: True - - DeprecatedShadowWidget: - - MDLabel: - text: "Doesn't require a lot of resources" - adaptive_size: True - - Box: - - MDLabel: - text: "New shadow rendering" - adaptive_size: True - - NewShadowWidget: - - MDLabel: - text: "It takes a lot of resources" - adaptive_size: True - ''' - - - class BaseShadowWidget(Widget): - pass - - - class DeprecatedShadowWidget(MDCard, BaseShadowWidget): - '''Deprecated shadow rendering. Doesn't require a lot of resources.''' - - - class NewShadowWidget(RectangularElevationBehavior, BaseShadowWidget, MDBoxLayout): - '''New shadow rendering. It takes a lot of resources.''' - - - class Example(MDApp): - def build(self): - return Builder.load_string(KV) - - - Example().run() - -.. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/elevation-differential.png - :align: center - - -For example, let's create an button with a rectangular elevation effect: - -.. code-block:: python - - from kivy.lang import Builder - from kivy.uix.behaviors import ButtonBehavior - - from kivymd.app import MDApp - from kivymd.uix.behaviors import ( - RectangularRippleBehavior, - BackgroundColorBehavior, - FakeRectangularElevationBehavior, - ) - - KV = ''' - : - size_hint: None, None - size: "250dp", "50dp" - - - MDScreen: - - # With elevation effect - RectangularElevationButton: - pos_hint: {"center_x": .5, "center_y": .6} - elevation: 18 - - # Without elevation effect - RectangularElevationButton: - pos_hint: {"center_x": .5, "center_y": .4} - ''' - - - class RectangularElevationButton( - RectangularRippleBehavior, - FakeRectangularElevationBehavior, - ButtonBehavior, - BackgroundColorBehavior, - ): - md_bg_color = [0, 0, 1, 1] - - - class Example(MDApp): - def build(self): - return Builder.load_string(KV) - - - Example().run() - -.. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/rectangular-elevation-effect.gif - :align: center - -Similarly, create a circular button: - -.. code-block:: python - - from kivy.lang import Builder - from kivy.uix.behaviors import ButtonBehavior - - from kivymd.uix.boxlayout import MDBoxLayout - from kivymd.app import MDApp - from kivymd.uix.behaviors import ( - CircularRippleBehavior, - FakeCircularElevationBehavior, - ) - - KV = ''' - : - size_hint: None, None - size: "100dp", "100dp" - radius: self.size[0] / 2 - md_bg_color: 0, 0, 1, 1 - - MDIcon: - icon: "hand-heart" - halign: "center" - valign: "center" - size: root.size - pos: root.pos - font_size: root.size[0] * .6 - theme_text_color: "Custom" - text_color: [1] * 4 - - - MDScreen: - - CircularElevationButton: - pos_hint: {"center_x": .5, "center_y": .6} - elevation: 24 - ''' - - - class CircularElevationButton( - FakeCircularElevationBehavior, - CircularRippleBehavior, - ButtonBehavior, - MDBoxLayout, - ): - pass - - - - class Example(MDApp): - def build(self): - return Builder.load_string(KV) - - - Example().run() - -.. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/circular-fake-elevation.png - :align: center - -Animating the elevation ------------------------ - -.. code-block:: python - - from kivy.animation import Animation - from kivy.lang import Builder - from kivy.properties import ObjectProperty - from kivy.uix.behaviors import ButtonBehavior - - from kivymd.app import MDApp - from kivymd.theming import ThemableBehavior - from kivymd.uix.behaviors import FakeRectangularElevationBehavior, RectangularRippleBehavior - from kivymd.uix.boxlayout import MDBoxLayout - - KV = ''' - MDFloatLayout: - - ElevatedWidget: - pos_hint: {'center_x': .5, 'center_y': .5} - size_hint: None, None - size: 100, 100 - md_bg_color: 0, 0, 1, 1 - ''' - - - class ElevatedWidget( - ThemableBehavior, - FakeRectangularElevationBehavior, - RectangularRippleBehavior, - ButtonBehavior, - MDBoxLayout, - ): - shadow_animation = ObjectProperty() - - def on_press(self, *args): - if self.shadow_animation: - Animation.cancel_all(self, "_elevation") - self.shadow_animation = Animation(_elevation=self.elevation + 10, d=0.4) - self.shadow_animation.start(self) - - def on_release(self, *args): - if self.shadow_animation: - Animation.cancel_all(self, "_elevation") - self.shadow_animation = Animation(_elevation=self.elevation, d=0.1) - self.shadow_animation.start(self) - - - class Example(MDApp): - def build(self): - return Builder.load_string(KV) - - - Example().run() - -.. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/rectangular-elevation-animation-effect.gif - :align: center - -Lighting position ------------------ - -.. code-block:: python - - from kivy.lang import Builder - - from kivymd.app import MDApp - from kivymd.uix.card import MDCard - from kivymd.uix.boxlayout import MDBoxLayout - from kivymd.uix.behaviors import RectangularElevationBehavior - - KV = ''' - MDScreen: - - ShadowCard: - pos_hint: {'center_x': .5, 'center_y': .5} - size_hint: None, None - size: 100, 100 - shadow_pos: -10 + slider.value, -10 + slider.value - elevation: 24 - md_bg_color: 1, 1, 1, 1 - - MDSlider: - id: slider - max: 20 - size_hint_x: .6 - pos_hint: {'center_x': .5, 'center_y': .3} - ''' - - - class ShadowCard(RectangularElevationBehavior, MDBoxLayout): - pass - - - class Example(MDApp): - def build(self): - return Builder.load_string(KV) - - - Example().run() - -.. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/shadow-pos.gif - :align: center -""" - -__all__ = ( - "CommonElevationBehavior", - "RectangularElevationBehavior", - "CircularElevationBehavior", - "RoundedRectangularElevationBehavior", - "ObservableShadow", - "FakeRectangularElevationBehavior", - "FakeCircularElevationBehavior", -) - -from io import BytesIO -from weakref import WeakMethod, ref - -from kivy import Logger -from kivy.clock import Clock -from kivy.core.image import Image as CoreImage -from kivy.lang import Builder -from kivy.metrics import dp -from kivy.properties import ( - AliasProperty, - BooleanProperty, - BoundedNumericProperty, - ListProperty, - NumericProperty, - ObjectProperty, - ReferenceListProperty, - StringProperty, - VariableListProperty, -) -from kivy.uix.widget import Widget -from PIL import Image, ImageDraw, ImageFilter - -from kivymd.app import MDApp - -Builder.load_string( - """ -#:import InstructionGroup kivy.graphics.instructions.InstructionGroup - - - - canvas.before: - # SOFT SHADOW - PushMatrix - Rotate: - angle: self.angle - origin: self._shadow_origin - Color: - group: "soft_shadow" - rgba: root.soft_shadow_cl - Rectangle: - group: "soft_shadow" - texture: self._soft_shadow_texture - size: self.soft_shadow_size - pos: self.soft_shadow_pos - PopMatrix - - # HARD SHADOW - PushMatrix - Rotate: - angle: self.angle - origin: self.center - Color: - group: "hard_shadow" - rgba: root.hard_shadow_cl - Rectangle: - group: "hard_shadow" - texture: self.hard_shadow_texture - size: self.hard_shadow_size - pos: self.hard_shadow_pos - PopMatrix - Color: - group: "shadow" - a: 1 -""", - filename="CommonElevationBehavior.kv", -) - - -class CommonElevationBehavior(Widget): - """Common base class for rectangular and circular elevation behavior.""" - - elevation = BoundedNumericProperty(0, min=0, errorvalue=0) - """ - Elevation of the widget. - - .. note:: - Although, this value does not represent the current elevation of the - widget. :attr:`~CommonElevationBehavior._elevation` can be used to - animate the current elevation and come back using the - :attr:`~CommonElevationBehavior.elevation` property directly. - - For example: + .. tab:: Declarative style with KV .. code-block:: python @@ -431,52 +24,41 @@ class CommonElevationBehavior(Widget): from kivy.uix.behaviors import ButtonBehavior from kivymd.app import MDApp - from kivymd.uix.behaviors import CircularElevationBehavior, CircularRippleBehavior - from kivymd.uix.boxlayout import MDBoxLayout + from kivymd.uix.behaviors import ( + RectangularRippleBehavior, + BackgroundColorBehavior, + CommonElevationBehavior, + ) KV = ''' - #:import Animation kivy.animation.Animation + + size_hint: None, None + size: "250dp", "50dp" - - size_hint: [None, None] - elevation: 6 - animation_: None - md_bg_color: [1] * 4 - on_size: - self.radius = [self.height / 2] * 4 - on_press: - if self.animation_: \ - self.animation_.cancel(self); \ - self.animation_ = Animation(_elevation=self.elevation + 6, d=0.08); \ - self.animation_.start(self) - on_release: - if self.animation_: \ - self.animation_.cancel(self); \ - self.animation_ = Animation(_elevation = self.elevation, d=0.08); \ - self.animation_.start(self) + MDScreen: - MDFloatLayout: + # With elevation effect + RectangularElevationButton: + pos_hint: {"center_x": .5, "center_y": .6} + elevation: 4.5 + shadow_offset: 0, 6 - WidgetWithShadow: - size: [root.size[1] / 2] * 2 - pos_hint: {"center": [0.5, 0.5]} + # Without elevation effect + RectangularElevationButton: + pos_hint: {"center_x": .5, "center_y": .4} ''' - class WidgetWithShadow( - CircularElevationBehavior, - CircularRippleBehavior, + class RectangularElevationButton( + RectangularRippleBehavior, + CommonElevationBehavior, ButtonBehavior, - MDBoxLayout, + BackgroundColorBehavior, ): def __init__(self, **kwargs): - # always set the elevation before the super().__init__ call - # self.elevation = 6 super().__init__(**kwargs) - - def on_size(self, *args): - self.radius = [self.size[0] / 2] + self.md_bg_color = "red" class Example(MDApp): @@ -486,990 +68,831 @@ class CommonElevationBehavior(Widget): Example().run() + .. tab:: Declarative python style + + .. code-block:: python + + from kivy.uix.behaviors import ButtonBehavior + + from kivymd.app import MDApp + from kivymd.uix.behaviors import ( + RectangularRippleBehavior, + BackgroundColorBehavior, + CommonElevationBehavior, + ) + from kivymd.uix.screen import MDScreen + + + class RectangularElevationButton( + RectangularRippleBehavior, + CommonElevationBehavior, + ButtonBehavior, + BackgroundColorBehavior, + ): + def __init__(self, **kwargs): + super().__init__(**kwargs) + self.md_bg_color = "red" + self.size_hint = (None, None) + self.size = ("250dp", "50dp") + + + class Example(MDApp): + def build(self): + return ( + MDScreen( + RectangularElevationButton( + pos_hint={"center_x": .5, "center_y": .6}, + elevation=4.5, + shadow_offset=(0, 6), + ), + RectangularElevationButton( + pos_hint={"center_x": .5, "center_y": .4}, + ), + ) + ) + + + Example().run() + +.. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/rectangular-elevation-effect.png + :align: center + +.. warning:: + + If before the KivyMD 1.1.0 library version you used the elevation property + with an average value of `12` for the shadow, then starting with the KivyMD + 1.1.0 library version, the average value of the elevation property will be + somewhere `4`. + +Similarly, create a circular button: + +.. tabs:: + + .. tab:: Declarative style with KV + + .. code-block:: python + + from kivy.lang import Builder + from kivy.uix.behaviors import ButtonBehavior + + from kivymd.app import MDApp + from kivymd.uix.behaviors import CircularRippleBehavior, CommonElevationBehavior + from kivymd.uix.floatlayout import MDFloatLayout + + KV = ''' + + size_hint: None, None + size: "100dp", "100dp" + radius: self.size[0] / 2 + shadow_radius: self.radius[0] + md_bg_color: "red" + + MDIcon: + icon: "hand-heart" + halign: "center" + valign: "center" + pos_hint: {"center_x": .5, "center_y": .5} + size: root.size + pos: root.pos + font_size: root.size[0] * .6 + theme_text_color: "Custom" + text_color: "white" + + + MDScreen: + + CircularElevationButton: + pos_hint: {"center_x": .5, "center_y": .6} + elevation: 4 + ''' + + + class CircularElevationButton( + CommonElevationBehavior, + CircularRippleBehavior, + ButtonBehavior, + MDFloatLayout, + ): + pass + + + class Example(MDApp): + def build(self): + return Builder.load_string(KV) + + + Example().run() + + .. tab:: Declarative python style + + .. code-block:: python + + from kivy.metrics import dp + from kivy.uix.behaviors import ButtonBehavior + + from kivymd.app import MDApp + from kivymd.uix.behaviors import CircularRippleBehavior, CommonElevationBehavior + from kivymd.uix.floatlayout import MDFloatLayout + from kivymd.uix.label import MDIcon + from kivymd.uix.screen import MDScreen + + + class CircularElevationButton( + CommonElevationBehavior, + CircularRippleBehavior, + ButtonBehavior, + MDFloatLayout, + ): + def __init__(self, **kwargs): + super().__init__(**kwargs) + self.size_hint = (None, None) + self.size = (dp(100), dp(100)) + self.radius = dp(100) / 2 + self.shadow_radius = dp(100) / 2 + self.md_bg_color = "red" + self.add_widget( + MDIcon( + icon="hand-heart", + halign="center", + valign="center", + pos_hint={"center_x": .5, "center_y": .5}, + size=self.size, + theme_text_color="Custom", + text_color="white", + font_size=self.size[0] * 0.6, + ) + ) + + + class Example(MDApp): + def build(self): + return ( + MDScreen( + CircularElevationButton( + pos_hint={"center_x": .5, "center_y": .5}, + elevation=4, + ) + ) + ) + + + Example().run() + +.. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/circular-elevation-effect.png + :align: center + +Animating the elevation +----------------------- + +.. tabs:: + + .. tab:: Declarative style with KV + + .. code-block:: python + + from kivy.animation import Animation + from kivy.lang import Builder + from kivy.uix.behaviors import ButtonBehavior + + from kivymd.app import MDApp + from kivymd.uix.behaviors import CommonElevationBehavior, RectangularRippleBehavior + from kivymd.uix.widget import MDWidget + + KV = ''' + MDScreen: + + ElevatedWidget: + pos_hint: {'center_x': .5, 'center_y': .5} + size_hint: None, None + size: 100, 100 + md_bg_color: 0, 0, 1, 1 + elevation: 4 + radius: 18 + ''' + + + class ElevatedWidget( + CommonElevationBehavior, + RectangularRippleBehavior, + ButtonBehavior, + MDWidget, + ): + _elev = 0 # previous elevation value + + def on_press(self, *args): + if not self._elev: + self._elev = self.elevation + Animation(elevation=self.elevation + 2, d=0.4).start(self) + + def on_release(self, *args): + Animation.cancel_all(self, "elevation") + Animation(elevation=self._elev, d=0.1).start(self) + + + class Example(MDApp): + def build(self): + return Builder.load_string(KV) + + + Example().run() + + .. tab:: Declarative python style + + .. code-block:: python + + from kivy.animation import Animation + from kivy.uix.behaviors import ButtonBehavior + + from kivymd.app import MDApp + from kivymd.uix.behaviors import CommonElevationBehavior, RectangularRippleBehavior + from kivymd.uix.screen import MDScreen + from kivymd.uix.widget import MDWidget + + + class ElevatedWidget( + CommonElevationBehavior, + RectangularRippleBehavior, + ButtonBehavior, + MDWidget, + ): + _elev = 0 # previous elevation value + + def on_press(self, *args): + if not self._elev: + self._elev = self.elevation + Animation(elevation=self.elevation + 2, d=0.4).start(self) + + def on_release(self, *args): + Animation.cancel_all(self, "elevation") + Animation(elevation=self._elev, d=0.1).start(self) + + + class Example(MDApp): + def build(self): + return ( + MDScreen( + ElevatedWidget( + pos_hint={'center_x': .5, 'center_y': .5}, + size_hint=(None, None), + size=(100, 100), + md_bg_color="blue", + elevation=4, + radius=18, + ) + ) + ) + + + Example().run() + +.. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/rectangular-elevation-animation-effect.gif + :align: center +""" + +from __future__ import annotations + +__all__ = ( + "CommonElevationBehavior", + "RectangularElevationBehavior", + "CircularElevationBehavior", + "RoundedRectangularElevationBehavior", + "FakeRectangularElevationBehavior", + "FakeCircularElevationBehavior", +) + +import os + +from kivy import Logger +from kivy.clock import Clock +from kivy.core.window import Window +from kivy.graphics import RenderContext, RoundedRectangle +from kivy.properties import ( + AliasProperty, + BooleanProperty, + BoundedNumericProperty, + ColorProperty, + ListProperty, + NumericProperty, + ObjectProperty, + VariableListProperty, +) +from kivy.uix.widget import Widget + +from kivymd import glsl_path + + +# FIXME: Add shadow manipulation with canvas instructions such as +# PushMatrix and PopMatrix. +class CommonElevationBehavior(Widget): + """Common base class for rectangular and circular elevation behavior.""" + + elevation = BoundedNumericProperty(0, min=0, errorvalue=0) + """ + Elevation of the widget. + :attr:`elevation` is an :class:`~kivy.properties.BoundedNumericProperty` and defaults to `0`. """ - # Shadow rendering properties. - # Shadow rotation memory - SHARED ACROSS OTHER CLASSES. - angle = NumericProperty(0) - """ - Angle of rotation in degrees of the current shadow. - This value is shared across different widgets. - - .. note:: - This value will affect both, hard and soft shadows. - Each shadow has his own origin point that's computed every time the - elevation changes. - - .. warning:: - Do not add `PushMatrix` inside the canvas before and add `PopMatrix` - in the next layer, this will cause visual errors, because the stack - used will clip the push and pop matrix already inside the canvas.before - canvas layer. - - Incorrect: - - .. code-block:: kv - - - canvas.before: - PushMatrix - [...] - canvas: - PopMatrix - - Correct: - - .. code-block:: kv - - - canvas.before: - PushMatrix - [...] - PopMatrix - - - - :attr:`angle` is an :class:`~kivy.properties.NumericProperty` - and defaults to `0`. - """ - - radius = VariableListProperty([0]) + shadow_radius = VariableListProperty([0], length=4) """ Radius of the corners of the shadow. - This values represents each corner of the shadow, starting from `top-left` - corner and going clockwise. + + .. versionadded:: 1.1.0 + + You don't have to use this parameter. + The radius of the elevation effect is calculated automatically one way + or another based on the radius of the parent widget, for example: .. code-block:: python - radius = [ - "top-left", - "top-right", - "bottom-right", - "bottom-left", - ] + from kivy.lang import Builder - This value can be expanded thus allowing this settings to be valid: + from kivymd.app import MDApp - .. code-block:: python + KV = ''' + MDScreen: - widget.radius=[0] # Translates to [0, 0, 0, 0] - widget.radius=[10, 3] # Translates to [10, 3, 10, 3] - widget.radius=[7.0, 8.7, 1.5, 3.0] # Translates to [7, 8, 1, 3] + MDCard: + radius: 12, 46, 12, 46 + size_hint: .5, .3 + pos_hint: {"center_x": .5, "center_y": .5} + elevation: 4 + shadow_softness: 8 + shadow_offset: (-2, 2) + ''' + + + class Test(MDApp): + def build(self): + return Builder.load_string(KV) + + + Test().run() + + .. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/shadow-radius.png + :align: center .. note:: - This value will affect both, hard and soft shadows. - This value only affects :class:`~RoundedRectangularElevationBehavior` - for now, but can be stored and used by custom shadow draw functions. + However, if you want to use this parameter, remember that the angle + values for the radius of the Kivy widgets and the radius for the shader + are different. - :attr:`radius` is an :class:`~kivy.properties.VariableListProperty` + .. code-block:: python + + shadow_radius = ['top-right', 'bot-right', 'top-left', 'bot-left'] + kivy_radius = ['top-left', 'top-right', 'bottom-right', 'bottom-left'] + + :attr:`shadow_radius` is an :class:`~kivy.properties.VariableListProperty` and defaults to `[0, 0, 0, 0]`. """ - # Position of the shadow. - _shadow_origin_x = NumericProperty(0) + shadow_softness = NumericProperty(12) """ - Shadow origin `x` position for the rotation origin. + Softness of the shadow. - Managed by `_shadow_origin`. - - :attr:`_shadow_origin_x` is an :class:`~kivy.properties.NumericProperty` - and defaults to `0`. - - .. note:: - This property is automatically processed. by _shadow_origin. - """ - - _shadow_origin_y = NumericProperty(0) - """ - Shadow origin y position for the rotation origin. - - Managed by :attr:`_shadow_origin`. - - :attr:`_shadow_origin_y` is an :class:`~kivy.properties.NumericProperty` - and defaults to `0`. - - .. note:: - This property is automatically processed. - """ - - _shadow_origin = ReferenceListProperty(_shadow_origin_x, _shadow_origin_y) - """ - Soft shadow rotation origin point. - - :attr:`_shadow_origin` is an :class:`~kivy.properties.ReferenceListProperty` - and defaults to `[0, 0]`. - - .. note:: - This property is automatically processed and relative to the canvas center. - """ - - _shadow_pos = ListProperty([0, 0]) # custom offset - """ - Soft shadow origin point. - - :attr:`_shadow_pos` is an :class:`~kivy.properties.ListProperty` - and defaults to `[0, 0]`. - - .. note:: - This property is automatically processed and relative to the widget's - canvas center. - """ - - shadow_pos = ListProperty([0, 0]) # bottom left corner - """ - Custom shadow origin point. If this property is set, :attr:`_shadow_pos` - will be ommited. - - This property allows users to fake light source. - - :attr:`shadow_pos` is an :class:`~kivy.properties.ListProperty` - and defaults to `[0, 0]`. - - .. note:: - this value overwrite the :attr:`_shadow_pos` processing. - """ - - # Shadow Group shared memory - __shadow_groups = {"global": []} - - shadow_group = StringProperty("global") - """ - Widget's shadow group. - By default every widget with a shadow is saved inside the memory - :attr:`__shadow_groups` as a weakref. This means that you can have multiple - light sources, one for every shadow group. - - To fake a light source use :attr:`force_shadow_pos`. - - :attr:`shadow_group` is an :class:`~kivy.properties.StringProperty` - and defaults to `"global"`. - """ - - _elevation = BoundedNumericProperty(0, min=0, errorvalue=0) - """ - Current elevation of the widget. - - .. warning:: - This property is the current elevation of the widget, do not - use this property directly, instead, use :class:`~CommonElevationBehavior` - elevation. - - :attr:`_elevation` is an :class:`~kivy.properties.NumericProperty` - and defaults to `0`. - """ - - # soft shadow - _soft_shadow_texture = ObjectProperty() - """ - Texture of the soft shadow texture for the canvas. - - :attr:`_soft_shadow_texture` is an :class:`~kivy.core.image.Image` - and defaults to `None`. - - .. note:: - This property is automatically processed. - """ - - soft_shadow_size = ListProperty([0, 0]) - """ - Size of the soft shadow texture over the canvas. - - :attr:`soft_shadow_size` is an :class:`~kivy.properties.ListProperty` - and defaults to `[0, 0]`. - - .. note:: - This property is automatically processed. - """ - - soft_shadow_pos = ListProperty([0, 0]) - """ - Position of the hard shadow texture over the canvas. - - :attr:`soft_shadow_pos` is an :class:`~kivy.properties.ListProperty` - and defaults to `[0, 0]`. - - .. note:: - This property is automatically processed. - """ - - soft_shadow_cl = ListProperty([0, 0, 0, 0.50]) - """ - Color of the soft shadow. - - :attr:`soft_shadow_cl` is an :class:`~kivy.properties.ListProperty` - and defaults to `[0, 0, 0, 0.15]`. - """ - - # hard shadow - hard_shadow_texture = ObjectProperty() - """ - Texture of the hard shadow texture for the canvas. - - :attr:`hard_shadow_texture` is an :class:`~kivy.core.image.Image` - and defaults to `None`. - - .. note:: - This property is automatically processed when elevation is changed. - """ - - hard_shadow_size = ListProperty([0, 0]) - """ - Size of the hard shadow texture over the canvas. - - :attr:`hard_shadow_size` is an :class:`~kivy.properties.ListProperty` - and defaults to `[0, 0]`. - - .. note:: - This property is automatically processed when elevation is changed. - """ - - hard_shadow_pos = ListProperty([0, 0]) - """ - Position of the hard shadow texture over the canvas. - - :attr:`hard_shadow_pos` is an :class:`~kivy.properties.ListProperty` - and defaults to `[0, 0]`. - - .. note:: - This property is automatically processed when elevation is changed. - """ - - hard_shadow_cl = ListProperty([0, 0, 0, 0.15]) - """ - Color of the hard shadow. - - .. note:: - :attr:`hard_shadow_cl` is an :class:`~kivy.properties.ListProperty` - and defaults to `[0, 0, 0, 0.15]`. - """ - - # Shared property for some calculations. - # This values are used to improve the gaussain blur and avoid that - # the blur goes outside the texture. - hard_shadow_offset = BoundedNumericProperty( - 2, min=0, errorhandler=lambda x: 0 if x < 0 else x - ) - """ - This value sets a special offset to the shadow canvas, this offset allows a - correct draw of the canvas size. allowing the effect to correctly blur the - image in the given space. - - :attr:`hard_shadow_offset` is an :class:`~kivy.properties.BoundedNumericProperty` - and defaults to `2`. - """ - - soft_shadow_offset = BoundedNumericProperty( - 4, min=0, errorhandler=lambda x: 0 if x < 0 else x - ) - """ - This value sets a special offset to the shadow canvas, this offset allows a - correct draw of the canvas size. allowing the effect to correctly blur the - image in the given space. - - :attr:`soft_shadow_offset` is an :class:`~kivy.properties.BoundedNumericProperty` - and defaults to `4`. - """ - - draw_shadow = ObjectProperty(None) - """ - This property controls the draw call of the context. - - This property is automatically set to :attr:`__draw_shadow__` inside the - `super().__init__ call.` unless the property is different of None. - - To set a different drawing instruction function, set this property before the - `super(),__init__` call inside the `__init__` definition of the new class. - - You can use the source for this classes as example of how to draw over - with the context: - - Real time shadows: - #. :class:`~RectangularElevationBehavior` - #. :class:`~CircularElevationBehavior` - #. :class:`~RoundedRectangularElevationBehavior` - #. :class:`~ObservableShadow` - - - Fake shadows (d`ont use this property): - #. :class:`~FakeRectangularElevationBehavior` - #. :class:`~FakeCircularElevationBehavior` - - :attr:`draw_shadow` is an :class:`~kivy.properties.ObjectProperty` - and defaults to `None`. - - .. note:: If this property is left to `None` the - :class:`~CommonElevationBehavior` will set to a function that will - raise a `NotImplementedError` inside `super().__init__`. - - Follow the next example to set a new draw instruction for the class - inside `__init__`: + .. versionadded:: 1.1.0 .. code-block:: python - class RoundedRectangularElevationBehavior(CommonElevationBehavior): - ''' - Shadow class for the RoundedRectangular shadow behavior. - Controls the size and position of the shadow. - ''' + from kivy.lang import Builder - def __init__(self, **kwargs): - self._draw_shadow = WeakMethod(self.__draw_shadow__) - super().__init__(**kwargs) + from kivymd.app import MDApp + from kivymd.uix.behaviors import BackgroundColorBehavior, CommonElevationBehavior - def __draw_shadow__(self, origin, end, context=None): - context.draw(...) + KV = ''' + + size_hint: None, None + size: "250dp", "50dp" - Context is a `Pillow` `ImageDraw` class. For more information check the - [Pillow official documentation](https://github.com/python-pillow/Pillow/). + + MDScreen: + + RectangularElevationButton: + pos_hint: {"center_x": .5, "center_y": .6} + elevation: 6 + shadow_softness: 6 + + RectangularElevationButton: + pos_hint: {"center_x": .5, "center_y": .4} + elevation: 6 + shadow_softness: 12 + ''' + + + class RectangularElevationButton(CommonElevationBehavior, BackgroundColorBehavior): + md_bg_color = [0, 0, 1, 1] + + + class Example(MDApp): + def build(self): + return Builder.load_string(KV) + + + Example().run() + + .. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/shadow-softness.png + :align: center + + :attr:`shadow_softness` is an :class:`~kivy.properties.NumericProperty` + and defaults to `12`. """ - # All classes that uses a fake shadow shall set this value as `True` - # for performance. - _fake_elevation = BooleanProperty(False) + shadow_offset = ListProperty((0, 2)) + """ + Offset of the shadow. + + .. versionadded:: 1.1.0 + + .. code-block:: python + + from kivy.lang import Builder + + from kivymd.app import MDApp + from kivymd.uix.behaviors import BackgroundColorBehavior, CommonElevationBehavior + + KV = ''' + + size_hint: None, None + size: "100dp", "100dp" + + + MDScreen: + + RectangularElevationButton: + pos_hint: {"center_x": .5, "center_y": .5} + elevation: 6 + shadow_radius: 18 + shadow_softness: 24 + shadow_offset: 12, 12 + ''' + + + class RectangularElevationButton(CommonElevationBehavior, BackgroundColorBehavior): + md_bg_color = [0, 0, 1, 1] + + + class Example(MDApp): + def build(self): + return Builder.load_string(KV) + + + Example().run() + + .. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/shadow-offset-1.png + :align: center + + .. code-block:: kv + + RectangularElevationButton: + shadow_offset: -12, 12 + + .. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/shadow-offset-2.png + :align: center + + .. code-block:: kv + + RectangularElevationButton: + shadow_offset: -12, -12 + + .. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/shadow-offset-3.png + :align: center + + .. code-block:: kv + + RectangularElevationButton: + shadow_offset: 12, -12 + + .. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/shadow-offset-4.png + :align: center + + :attr:`shadow_offset` is an :class:`~kivy.properties.ListProperty` + and defaults to `(0, 2)`. + """ + + shadow_color = ColorProperty([0, 0, 0, 0.6]) + """ + Offset of the shadow. + + .. versionadded:: 1.1.0 + + .. code-block:: python + + RectangularElevationButton: + shadow_color: 0, 0, 1, .8 + + .. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/shadow-color.png + :align: center + + :attr:`shadow_color` is an :class:`~kivy.properties.ColorProperty` + and defaults to `[0.4, 0.4, 0.4, 0.8]`. + """ + + _transition_ref = ObjectProperty() + _has_relative_position = BooleanProperty(defaultvalue=False) + _elevation = 0 + _shadow_color = [0.0, 0.0, 0.0, 0.0] + + def _get_window_pos(self, *args): + window_pos = self.to_window(*self.pos) + # To list, so it can be compared to self.pos directly. + return [window_pos[0], window_pos[1]] + + def _set_window_pos(self, value): + self.window_pos = value + + window_pos = AliasProperty(_get_window_pos, _set_window_pos) def __init__(self, **kwargs): - if self.draw_shadow is None: - self.draw_shadow = WeakMethod(self.__draw_shadow__) - self.prev_shadow_group = None - im = BytesIO() - Image.new("RGBA", (4, 4), color=(0, 0, 0, 0)).save(im, format="png") - im.seek(0) - # Setting a empty image as texture, improves performance. - self._soft_shadow_texture = self.hard_shadow_texture = CoreImage( - im, ext="png" - ).texture - Clock.schedule_once(self.shadow_preset, -1) - self.on_shadow_group(self, self.shadow_group) - - self.bind( - pos=self._update_shadow, - size=self._update_shadow, - radius=self._update_shadow, - ) super().__init__(**kwargs) - def on_shadow_group(self, instance, value): + with self.canvas.before: + self.context = RenderContext(use_parent_projection=True) + with self.context: + self.rect = RoundedRectangle(pos=self.pos, size=self.size) + + Clock.schedule_once(self.after_init) + + def after_init(self, *args): + Clock.schedule_once(self.check_for_relative_behavior) + Clock.schedule_once(self.set_shader_string) + Clock.schedule_once(lambda x: self.on_elevation(self, self.elevation)) + self.on_pos() + + def check_for_relative_behavior(self, *args) -> None: """ - This function controls the shadow group of the widget. - Do not use Directly to change the group. instead, use the shadow_group - :attr:`property`. + Checks if the widget has relative properties and if necessary + binds Window.on_draw and screen events to fix behavior """ - groups = CommonElevationBehavior.__shadow_groups - if self.prev_shadow_group: - group = groups[self.prev_shadow_group] - for widget in group[:]: - if widget() is self: - group.remove(widget) - group = self.prev_shadow_group = self.shadow_group - if group not in groups: - groups[group] = [] - r = ref(self, CommonElevationBehavior._clear_shadow_groups) - groups[group].append(r) + if self.pos != self.window_pos: + self._has_relative_position = True - @staticmethod - def _clear_shadow_groups(wk): - # auto flush the element when the weak reference have been deleted - groups = CommonElevationBehavior.__shadow_groups - for group in list(groups.values()): - if not group: - break - if wk in group: - group.remove(wk) + # Loops to check if its inside screenmanager or bottom_navigation. + widget = self + while True: + # Checks if has screen event function + # works for Screen and MDTab objects. + if hasattr(widget, "on_pre_enter"): + widget.bind(on_pre_enter=self.apply_correction) + widget.bind(on_pre_leave=self.apply_correction) + widget.bind(on_enter=self.reset_correction) + widget.bind(on_leave=self.reset_correction) + self._has_relative_position = True + + # Save refs to objects with transition property. + if hasattr(widget, "header"): # specific to bottom_nav + self._transition_ref = widget.header.panel + elif hasattr(widget, "manager"): # specific to screen + if widget.manager: # manager cant be None + self._transition_ref = widget.manager break - def force_shadow_pos(self, shadow_pos): - """ - This property forces the shadow position in every widget inside the - widget. The argument :attr:`shadow_pos` is expected as a - or . - """ - - if self.shadow_group is None: - return - group = CommonElevationBehavior.__shadow_groups[self.shadow_group] - for wk in group[:]: - widget = wk() - if widget is None: - group.remove(wk) - widget.shadow_pos = shadow_pos - del group - - def update_group_property(self, property_name, value): - """ - This functions allows to change properties of every widget inside the - shadow group. - """ - - if self.shadow_group is None: - return - group = CommonElevationBehavior.__shadow_groups[self.shadow_group] - for wk in group[:]: - widget = wk() - if widget is None: - group.remove(wk) - setattr(widget, property_name, value) - del group - - def shadow_preset(self, *args): - """ - This function is meant to set the default configuration of the - elevation. - - After a new instance is created, the elevation property will be launched - and thus this function will update the elevation if the KV lang have not - done it already. - - Works similar to an `__after_init__` call inside a widget. - """ - - from kivymd.uix.card import MDCard - - if self.elevation is None and not issubclass(self.__class__, MDCard): - self.elevation = 10 - if self._fake_elevation is False: - self._update_shadow(self, self.elevation) - self.bind( - pos=self._update_shadow, - size=self._update_shadow, - _elevation=self._update_shadow, - ) - - def on_elevation(self, instance, value): - """ - Elevation event that sets the current elevation value to `_elevation`. - """ - - if value is not None: - self._elevation = value - - def _set_soft_shadow_a(self, value): - value = 0 if value < 0 else (1 if value > 1 else value) - self.soft_shadow_cl[-1] = value - return True - - def _set_hard_shadow_a(self, value): - value = 0 if value < 0 else (1 if value > 1 else value) - self.hard_shadow_cl[-1] = value - return True - - def _get_soft_shadow_a(self): - return self.soft_shadow_cl[-1] - - def _get_hard_shadow_a(self): - return self.hard_shadow_cl[-1] - - _soft_shadow_a = AliasProperty( - _get_soft_shadow_a, _set_soft_shadow_a, bind=["soft_shadow_cl"] - ) - _hard_shadow_a = AliasProperty( - _get_hard_shadow_a, _set_hard_shadow_a, bind=["hard_shadow_cl"] - ) - - def on_disabled(self, instance, value): - """ - This function hides the shadow when the widget is disabled. - It sets the shadow to `0`. - """ - - if self.disabled is True: - self._elevation = 0 - else: - self._elevation = 0 if self.elevation is None else self.elevation - self._update_shadow(self, self._elevation) - try: - super().on_disabled(instance, value) - except Exception: - pass - - def _update_elevation(self, instance, value): - self._elevation = value - self._update_shadow(instance, value) - - def _update_shadow_pos(self, instance, value): - if self._elevation > 0: - self.hard_shadow_pos = [ - self.x - dp(self.hard_shadow_offset), # + self.shadow_pos[0], - self.y - dp(self.hard_shadow_offset), # + self.shadow_pos[1], - ] - if self.shadow_pos == [0, 0]: - self.soft_shadow_pos = [ - self.x - + self._shadow_pos[0] - - self._elevation - - dp(self.soft_shadow_offset), - self.y - + self._shadow_pos[1] - - self._elevation - - dp(self.soft_shadow_offset), - ] + elif widget.parent and str(widget) != str(widget.parent): + widget = widget.parent else: - self.soft_shadow_pos = [ - self.x - + self.shadow_pos[0] - - self._elevation - - dp(self.soft_shadow_offset), - self.y - + self.shadow_pos[1] - - self._elevation - - dp(self.soft_shadow_offset), - ] - self._shadow_origin = [ - self.soft_shadow_pos[0] + self.soft_shadow_size[0] / 2, - self.soft_shadow_pos[1] + self.soft_shadow_size[1] / 2, + break + + if self._has_relative_position: + Window.bind(on_draw=self.update_window_position) + + def apply_correction(self, *args): + if self._transition_ref: + transition = str(self._transition_ref.transition) + # Slide and Card transitions only need _has_relative_pos to be + # always on. + if ( + "SlideTransition" in transition + or "CardTransition" in transition + ): + self.context.use_parent_modelview = False + else: + self.context.use_parent_modelview = True + + def reset_correction(self, *args): + self.context.use_parent_modelview = False + self.update_window_position() + + def get_shader_string(self) -> str: + shader_string = "" + for name_file in ["header.frag", "elevation.frag", "main.frag"]: + with open( + os.path.join(glsl_path, "elevation", name_file), + encoding="utf-8", + ) as file: + shader_string += f"{file.read()}\n\n" + + return shader_string + + def set_shader_string(self, *args) -> None: + self.context["shadow_radius"] = list(map(float, self.shadow_radius)) + self.context["shadow_softness"] = float(self.shadow_softness) + self.context["shadow_color"] = list(map(float, self.shadow_color))[ + :-1 + ] + [float(self.opacity)] + self.context["pos"] = list(map(float, self.rect.pos)) + self.context.shader.fs = self.get_shader_string() + + def update_resolution(self) -> None: + self.context["resolution"] = (*self.rect.size, *self.rect.pos) + + def on_shadow_color(self, instance, value) -> None: + def on_shadow_color(*args): + self._shadow_color = list(map(float, value))[:-1] + [ + float(self.opacity) if not self.disabled else 0 ] + self.context["shadow_color"] = self._shadow_color - def on__shadow_pos(self, ins, val): - """ - Updates the shadow with the computed value. + Clock.schedule_once(on_shadow_color) - Call this function every time you need to force a shadow update. - """ + def on_shadow_radius(self, instance, value) -> None: + def on_shadow_radius(*args): + if hasattr(self, "context"): + self.context["shadow_radius"] = list(map(float, value)) - self._update_shadow_pos(ins, val) + Clock.schedule_once(on_shadow_radius) - def on_shadow_pos(self, ins, val): - """ - Updates the shadow with the fixed value. + def on_shadow_softness(self, instance, value) -> None: + def on_shadow_softness(*args): + if hasattr(self, "context"): + self.context["shadow_softness"] = float(value) - Call this function every time you need to force a shadow update. - """ + Clock.schedule_once(on_shadow_softness) - self._update_shadow_pos(ins, val) - - def _update_shadow(self, instance, value): - self._update_shadow_pos(instance, value) - if self._elevation > 0 and self._fake_elevation is False: - # dynamic elevation position for the shadow - if self.shadow_pos == [0, 0]: - self._shadow_pos = [0, -self._elevation * 0.4] - - # HARD Shadow - offset = int(dp(self.hard_shadow_offset)) - size = [ - int(self.size[0] + (offset * 2)), - int(self.size[1] + (offset * 2)), - ] - im = BytesIO() - # context - img = Image.new("RGBA", tuple(size), color=(0, 0, 0, 0)) - # draw context - shadow = ImageDraw.Draw(img) - self.draw_shadow()( - [offset, offset], - [ - int(size[0] - 1 - offset), - int(size[1] - 1 - offset), - ], - context=shadow - # context=ref(shadow) - ) - img = img.filter( - ImageFilter.GaussianBlur( - radius=int(dp(1 + self.hard_shadow_offset / 3)) + def on_elevation(self, instance, value) -> None: + def on_elevation(*args): + if hasattr(self, "context"): + self._elevation = value + self.hide_elevation( + True if (value <= 0 or self.disabled) else False ) - ) - img.save(im, format="png") - im.seek(0) - self.hard_shadow_size = size - self.hard_shadow_texture = CoreImage(im, ext="png").texture - # soft shadow - if self.soft_shadow_cl[-1] > 0: - offset = dp(self.soft_shadow_offset) - size = [ - int(self.size[0] + dp(self._elevation * 2) + (offset * 2)), - int(self.size[1] + dp(self._elevation * 2) + (offset * 2)), - # ((self._elevation)*2) + x + (offset*2)) for x in self.size - ] - im = BytesIO() - img = Image.new("RGBA", tuple(size), color=((0,) * 4)) - shadow = ImageDraw.Draw(img) - _offset = int(dp(self._elevation + offset)) - self.draw_shadow()( - [ - _offset, - _offset, - ], - [int(size[0] - _offset - 1), int(size[1] - _offset - 1)], - context=shadow - # context=ref(shadow) - ) - img = img.filter( - ImageFilter.GaussianBlur(radius=self._elevation // 2) - ) - shadow = ImageDraw.Draw(img) - img.save(im, format="png") - im.seek(0) - self.soft_shadow_size = size - self._soft_shadow_texture = CoreImage(im, ext="png").texture - else: - im = BytesIO() - Image.new("RGBA", (4, 4), color=(0, 0, 0, 0)).save(im, format="png") - im.seek(0) - self._soft_shadow_texture = self.hard_shadow_texture = CoreImage( - im, ext="png" - ).texture + Clock.schedule_once(on_elevation) + + def on_shadow_offset(self, instance, value) -> None: + self.on_size() + self.on_pos() + + def update_window_position(self, *args) -> None: + """ + This function is used only when the widget has relative position + properties. + """ + + self.on_pos() + + def on_pos(self, *args) -> None: + if not hasattr(self, "rect"): return - def _get_center(self): - center = [self.pos[0] + self.width / 2, self.pos[1] + self.height / 2] - return center + if ( + self._has_relative_position + and not self.context.use_parent_modelview + ): + pos = self.window_pos + else: + pos = self.pos - def __draw_shadow__(self, origin, end, context=None): - Logger.warning( - f"KivyMD: " - f"If you see this error, this means that either youre using " - f"`CommonElevationBehavio`r directly or your 'shader' dont have a " - f"`_draw_shadow` instruction, remember to overwrite this function" - f"to draw over the image context. Тhe figure you would like. " - f"Or your class {self.__class__.__name__} is not inherited from " - f"any of the classes {__all__}" + self.rect.pos = [ + pos[0] + - ((self.rect.size[0] - self.width) / 2) + - self.shadow_offset[0], + pos[1] + - ((self.rect.size[1] - self.height) / 2) + - self.shadow_offset[1], + ] + + self.context["mouse"] = [self.rect.pos[0], 0.0, 0.0, 0.0] + self.context["pos"] = list(map(float, self.rect.pos)) + self.update_resolution() + + def on_size(self, *args) -> None: + if not hasattr(self, "rect"): + return + + # If the elevation value is 0, set the canvas size to zero. + # Because even with a zero elevation value, the shadow is displayed + # under the widget. This is visible if we change the scale + # of the widget. + width = self.size[0] if self.elevation else 0 + height = self.size[1] if self.elevation else 0 + self.rect.size = ( + width + (self._elevation * self.shadow_softness / 2), + height + (self._elevation * self.shadow_softness / 2), ) + self.context["mouse"] = [self.rect.pos[0], 0.0, 0.0, 0.0] + self.context["size"] = list(map(float, self.rect.size)) + self.update_resolution() + + def on_opacity(self, instance, value: int | float) -> None: + """ + Adjusts the transparency of the shadow according to the transparency + of the widget. + """ + + def on_opacity(*args): + self._shadow_color = list(map(float, self._shadow_color))[:-1] + [ + float(value) + ] + self.context["shadow_color"] = self._shadow_color + + super().on_opacity(instance, value) + Clock.schedule_once(on_opacity) + + def on_radius(self, instance, value) -> None: + self.shadow_radius = [value[1], value[2], value[0], value[3]] + + def on_disabled(self, instance, value) -> None: + if value: + self._elevation = 0 + self.hide_elevation(True) + else: + self.hide_elevation(False) + + def hide_elevation(self, hide: bool) -> None: + if hide: + self._elevation = -self.elevation + self._shadow_color = [0.0, 0.0, 0.0, 0.0] + else: + self._elevation = self.elevation + self._shadow_color = self.shadow_color[:-1] + [float(self.opacity)] + + self.on_shadow_color(self, self._shadow_color) + self.on_size() + self.on_pos() + class RectangularElevationBehavior(CommonElevationBehavior): """ - Base class for a rectangular elevation behavior. + .. deprecated:: 1.1.0 + Use :class:`~CommonElevationBehavior` class instead. """ def __init__(self, **kwargs): - self.draw_shadow = WeakMethod(self.__draw_shadow__) super().__init__(**kwargs) - - def __draw_shadow__(self, origin, end, context=None): - context.rectangle(origin + end, fill=tuple([255] * 4)) + Logger.warning( + "KivyMD: " + "The `RectangularElevationBehavior` class has been deprecated. " + "Use the `CommonElevationBehavior` class instead.`" + ) class CircularElevationBehavior(CommonElevationBehavior): """ - Base class for a circular elevation behavior. + .. deprecated:: 1.1.0 + Use :class:`~CommonElevationBehavior` class instead. """ def __init__(self, **kwargs): - self.draw_shadow = WeakMethod(self.__draw_shadow__) super().__init__(**kwargs) - - def __draw_shadow__(self, origin, end, context=None): - context.ellipse(origin + end, fill=tuple([255] * 4)) + Logger.warning( + "KivyMD: " + "The `CircularElevationBehavior` class has been deprecated. " + "Use the `CommonElevationBehavior` class instead.`" + ) class RoundedRectangularElevationBehavior(CommonElevationBehavior): """ - Base class for rounded rectangular elevation behavior. + .. deprecated:: 1.1.0 + Use :class:`~CommonElevationBehavior` class instead. """ def __init__(self, **kwargs): - self.bind( - radius=self._update_shadow, - ) - self.draw_shadow = WeakMethod(self.__draw_shadow__) super().__init__(**kwargs) - - def __draw_shadow__(self, origin, end, context=None): - if self.radius == [0, 0, 0, 0]: - context.rectangle(origin + end, fill=tuple([255] * 4)) - else: - radius = [x * 2 for x in self.radius] - context.pieslice( - [ - origin[0], - origin[1], - origin[0] + radius[0], - origin[1] + radius[0], - ], - 180, - 270, - fill=(255, 255, 255, 255), - ) - context.pieslice( - [ - end[0] - radius[1], - origin[1], - end[0], - origin[1] + radius[1], - ], - 270, - 360, - fill=(255, 255, 255, 255), - ) - context.pieslice( - [ - end[0] - radius[2], - end[1] - radius[2], - end[0], - end[1], - ], - 0, - 90, - fill=(255, 255, 255, 255), - ) - context.pieslice( - [ - origin[0], - end[1] - radius[3], - origin[0] + radius[3], - end[1], - ], - 90, - 180, - fill=(255, 255, 255, 255), - ) - if all((x == self.radius[0] for x in self.radius)): - radius = int(self.radius[0]) - context.rectangle( - [ - origin[0] + radius, - origin[1], - end[0] - radius, - end[1], - ], - fill=(255,) * 4, - ) - context.rectangle( - [ - origin[0], - origin[1] + radius, - end[0], - end[1] - radius, - ], - fill=(255,) * 4, - ) - else: - radius = [ - max((self.radius[0], self.radius[1])), - max((self.radius[1], self.radius[2])), - max((self.radius[2], self.radius[3])), - max((self.radius[3], self.radius[0])), - ] - context.rectangle( - [ - origin[0] + self.radius[0], - origin[1], - end[0] - self.radius[1], - end[1] - radius[2], - ], - fill=(255,) * 4, - ) - context.rectangle( - [ - origin[0] + radius[3], - origin[1] + self.radius[1], - end[0], - end[1] - self.radius[2], - ], - fill=(255,) * 4, - ) - context.rectangle( - [ - origin[0] + self.radius[3], - origin[1] + radius[0], - end[0] - self.radius[2], - end[1], - ], - fill=(255,) * 4, - ) - context.rectangle( - [ - origin[0], - origin[1] + self.radius[0], - end[0] - radius[2], - end[1] - self.radius[3], - ], - fill=(255,) * 4, - ) - - -class ObservableShadow(CommonElevationBehavior): - """ - ObservableShadow is real time shadow render that it's intended to only - render a partial shadow of widgets based upon on the window observable - area, this is meant to improve the performance of bigger widgets. - - .. warning:: - This is an empty class, the name has been reserved for future use. - if you include this clas in your object, you wil get a - `NotImplementedError`. - """ - - def __init__(self, **kwargs): - # self._shadow = MDApp.get_running_app().theme_cls.round_shadow - # self._fake_elevation=True - raise NotImplementedError( - "ObservableShadow:\n\t" "This class is in current development" + Logger.warning( + "KivyMD: " + "The `RoundedRectangularElevationBehavior` class has been " + "deprecated. Use the `CommonElevationBehavior` class instead.`" ) - super().__init__(**kwargs) class FakeRectangularElevationBehavior(CommonElevationBehavior): """ - `FakeRectangularElevationBehavio`r is a shadow mockup for widgets. Improves - performance using cached images inside `kivymd.images` dir - - This class cast a fake Rectangular shadow behaind the widget. - - You can either use this behavior to overwrite the elevation of a prefab - widget, or use it directly inside a new widget class definition. - - Use this class as follows for new widgets: - - .. code-block:: python - - class NewWidget( - ThemableBehavior, - FakeCircularElevationBehavior, - SpecificBackgroundColorBehavior, - # here you add the other front end classes for the widget front_end, - ): - [...] - - With this method each class can draw it's content in the canvas in the - correct order, avoiding some visual errors. - - `FakeCircularElevationBehavior` will load prefabricated textures to - optimize loading times. - - .. note:: About rounded corners: - be careful, since this behavior is a mockup and will not draw any - rounded corners. + .. deprecated:: 1.1.0 + Use :class:`~CommonElevationBehavior` class instead. """ def __init__(self, **kwargs): - # self._shadow = MDApp.get_running_app().theme_cls.round_shadow - self.draw_shadow = WeakMethod(self.__draw_shadow__) - self._fake_elevation = True - self._update_shadow(self, self.elevation) super().__init__(**kwargs) - - def _update_shadow(self, *args): - if self._elevation > 0: - # Set shadow size. - ratio = self.width / (self.height if self.height != 0 else 1) - if -2 < ratio < 2: - self._shadow = MDApp.get_running_app().theme_cls.quad_shadow - width = soft_width = self.width * 1.9 - height = soft_height = self.height * 1.9 - elif ratio <= -2: - self._shadow = MDApp.get_running_app().theme_cls.rec_st_shadow - ratio = abs(ratio) - if ratio > 5: - ratio = ratio * 22 - else: - ratio = ratio * 11.5 - width = soft_width = self.width * 1.9 - height = self.height + dp(ratio) - soft_height = ( - self.height + dp(ratio) + dp(self._elevation) * 0.5 - ) - else: - self._shadow = MDApp.get_running_app().theme_cls.quad_shadow - width = soft_width = self.width * 1.8 - height = soft_height = self.height * 1.8 - - self.soft_shadow_size = (soft_width, soft_height) - self.hard_shadow_size = (width, height) - # Set ``soft_shadow`` parameters. - center_x, center_y = self._get_center() - self.hard_shadow_pos = self.soft_shadow_pos = ( - center_x - soft_width / 2, - center_y - soft_height / 2 - dp(self._elevation * 0.5), - ) - # Set transparency - self._soft_shadow_a = 0.1 * 1.05**self._elevation - self._hard_shadow_a = 0.4 * 0.8**self._elevation - t = int(round(self._elevation)) - if 0 < t <= 23: - self._soft_shadow_texture = ( - self._hard_shadow_texture - ) = self._shadow.textures[str(t)] - else: - self._soft_shadow_texture = ( - self._hard_shadow_texture - ) = self._shadow.textures["23"] - else: - self._soft_shadow_a = 0 - self._hard_shadow_a = 0 - - def __draw_shadow__(self, origin, end, context=None): - pass + Logger.warning( + "KivyMD: " + "The `FakeRectangularElevationBehavior` class has been " + "deprecated. Use the `CommonElevationBehavior` class instead." + ) class FakeCircularElevationBehavior(CommonElevationBehavior): """ - `FakeCircularElevationBehavior` is a shadow mockup for widgets. Improves - performance using cached images inside `kivymd.images` dir - - This class cast a fake elliptic shadow behaind the widget. - - You can either use this behavior to overwrite the elevation of a prefab - widget, or use it directly inside a new widget class definition. - - Use this class as follows for new widgets: - - .. code-block:: python - - class NewWidget( - ThemableBehavior, - FakeCircularElevationBehavior, - SpecificBackgroundColorBehavior, - # here you add the other front end classes for the widget front_end, - ): - [...] - - With this method each class can draw it's content in the canvas in the - correct order, avoiding some visual errors. - - `FakeCircularElevationBehavior` will load prefabricated textures to optimize - loading times. - - .. note:: About rounded corners: - be careful, since this behavior is a mockup and will not draw any rounded - corners. only perfect ellipses. + .. deprecated:: 1.1.0 + Use :class:`~CommonElevationBehavior` class instead. """ def __init__(self, **kwargs): - self._shadow = MDApp.get_running_app().theme_cls.round_shadow - self.draw_shadow = WeakMethod(self.__draw_shadow__) - self._fake_elevation = True - self._update_shadow(self, self.elevation) super().__init__(**kwargs) - - def _update_shadow(self, *args): - if self._elevation > 0: - # set shadow size - width = self.width * 2 - height = self.height * 2 - center_x, center_y = self._get_center() - - x = center_x - width / 2 - self.soft_shadow_size = (width, height) - self.hard_shadow_size = (width, height) - # set ``soft_shadow`` parameters - y = center_y - height / 2 - dp(0.5 * self._elevation) - self.soft_shadow_pos = (x, y) - - # set ``hard_shadow`` parameters - y = center_y - height / 2 - dp(0.5 * self._elevation) - self.hard_shadow_pos = (x, y) - - # shadow transparency - self._soft_shadow_a = 0.1 * 1.05**self._elevation - self._hard_shadow_a = 0.4 * 0.8**self._elevation - t = int(round(self._elevation)) - if 0 < t <= 23: - if hasattr(self, "_shadow"): - self._soft_shadow_texture = ( - self._hard_shadow_texture - ) = self._shadow.textures[str(t)] - else: - self._soft_shadow_texture = ( - self._hard_shadow_texture - ) = self._shadow.textures["23"] - else: - self._soft_shadow_a = 0 - self._hard_shadow_a = 0 - - def __draw_shadow__(self, origin, end, context=None): - pass + Logger.warning( + "KivyMD: " + "The `FakeCircularElevationBehavior` class has been deprecated. " + "Use the `CommonElevationBehavior` class instead." + ) diff --git a/sbapp/kivymd/uix/behaviors/focus_behavior.py b/sbapp/kivymd/uix/behaviors/focus_behavior.py index 15d3169..b90f16a 100644 --- a/sbapp/kivymd/uix/behaviors/focus_behavior.py +++ b/sbapp/kivymd/uix/behaviors/focus_behavior.py @@ -100,23 +100,35 @@ class FocusBehavior(HoverBehavior, ButtonBehavior): def on_enter(self): """Called when mouse enter the bbox of the widget.""" - if hasattr(self, "md_bg_color") and self.focus_behavior: + if ( + hasattr(self, "md_bg_color") or hasattr(self, "bg_color") + ) and self.focus_behavior: if hasattr(self, "theme_cls") and not self.focus_color: - self.md_bg_color = self.theme_cls.bg_normal + color = self.theme_cls.bg_normal else: if not self.focus_color: - self.md_bg_color = App.get_running_app().theme_cls.bg_normal + color = App.get_running_app().theme_cls.bg_normal else: - self.md_bg_color = self.focus_color + color = self.focus_color + self._set_bg_color(color) def on_leave(self): """Called when the mouse exit the widget.""" - if hasattr(self, "md_bg_color") and self.focus_behavior: + if ( + hasattr(self, "md_bg_color") or hasattr(self, "bg_color") + ) and self.focus_behavior: if hasattr(self, "theme_cls") and not self.unfocus_color: - self.md_bg_color = self.theme_cls.bg_light + color = self.theme_cls.bg_light else: if not self.unfocus_color: - self.md_bg_color = App.get_running_app().theme_cls.bg_light + color = App.get_running_app().theme_cls.bg_light else: - self.md_bg_color = self.unfocus_color + color = self.unfocus_color + self._set_bg_color(color) + + def _set_bg_color(self, color): + if hasattr(self, "md_bg_color"): + self.md_bg_color = color + elif hasattr(self, "bg_color"): + self.bg_color = color diff --git a/sbapp/kivymd/uix/behaviors/ripple_behavior.py b/sbapp/kivymd/uix/behaviors/ripple_behavior.py index f06e724..1271878 100755 --- a/sbapp/kivymd/uix/behaviors/ripple_behavior.py +++ b/sbapp/kivymd/uix/behaviors/ripple_behavior.py @@ -111,7 +111,7 @@ from kivy.properties import ( from kivy.uix.behaviors import ToggleButtonBehavior -class CommonRipple(object): +class CommonRipple: """Base class for ripple effect.""" ripple_rad_default = NumericProperty(1) diff --git a/sbapp/kivymd/uix/behaviors/rotate_behavior.py b/sbapp/kivymd/uix/behaviors/rotate_behavior.py new file mode 100644 index 0000000..c80b879 --- /dev/null +++ b/sbapp/kivymd/uix/behaviors/rotate_behavior.py @@ -0,0 +1,133 @@ +""" +Behaviors/Rotate +================ + +.. versionadded:: 1.1.0 + +Base class for controlling the rotate of the widget. + +.. note:: See `kivy.graphics.Rotate + `_ + for more information. + +Kivy +---- + +.. code-block:: python + + from kivy.animation import Animation + from kivy.lang import Builder + from kivy.app import App + from kivy.properties import NumericProperty + from kivy.uix.button import Button + + KV = ''' + Screen: + + RotateButton: + size_hint: .5, .5 + pos_hint: {"center_x": .5, "center_y": .5} + on_release: app.change_rotate(self) + + canvas.before: + PushMatrix + Rotate: + angle: self.rotate_value_angle + axis: 0, 0, 1 + origin: self.center + canvas.after: + PopMatrix + ''' + + + class RotateButton(Button): + rotate_value_angle = NumericProperty(0) + + + class Test(App): + def build(self): + return Builder.load_string(KV) + + def change_rotate(self, instance_button: Button) -> None: + Animation(rotate_value_angle=45, d=0.3).start(instance_button) + + + Test().run() + +KivyMD +------ + +.. code-block:: python + + from kivy.animation import Animation + from kivy.lang import Builder + from kivy.uix.behaviors import ButtonBehavior + + from kivymd.app import MDApp + from kivymd.uix.behaviors import RotateBehavior + from kivymd.uix.boxlayout import MDBoxLayout + + KV = ''' + MDScreen: + + RotateBox: + size_hint: .5, .5 + pos_hint: {"center_x": .5, "center_y": .5} + on_release: app.change_rotate(self) + md_bg_color: "red" + ''' + + + class RotateBox(ButtonBehavior, RotateBehavior, MDBoxLayout): + pass + + + class Test(MDApp): + def build(self): + return Builder.load_string(KV) + + def change_rotate(self, instance_button: RotateBox) -> None: + Animation(rotate_value_angle=45, d=0.3).start(instance_button) + + + Test().run() +""" + +__all__ = ("RotateBehavior",) + +from kivy.lang import Builder +from kivy.properties import ListProperty, NumericProperty + +Builder.load_string( + """ + + canvas.before: + PushMatrix + Rotate: + angle: self.rotate_value_angle + axis: tuple(self.rotate_value_axis) + origin: self.center + canvas.after: + PopMatrix +""" +) + + +class RotateBehavior: + """Base class for controlling the rotate of the widget.""" + + rotate_value_angle = NumericProperty(0) + """ + Property for getting/setting the angle of the rotation. + + :attr:`rotate_value_angle` is an :class:`~kivy.properties.NumericProperty` + and defaults to `0`. + """ + + rotate_value_axis = ListProperty((0, 0, 1)) + """ + Property for getting/setting the axis of the rotation. + + :attr:`rotate_value_axis` is an :class:`~kivy.properties.ListProperty` + and defaults to `(0, 0, 1)`. + """ diff --git a/sbapp/kivymd/uix/behaviors/scale_behavior.py b/sbapp/kivymd/uix/behaviors/scale_behavior.py new file mode 100644 index 0000000..bfc1e03 --- /dev/null +++ b/sbapp/kivymd/uix/behaviors/scale_behavior.py @@ -0,0 +1,156 @@ +""" +Behaviors/Scale +=============== + +.. versionadded:: 1.1.0 + +Base class for controlling the scale of the widget. + +.. note:: See `kivy.graphics.Rotate + `_ + for more information. + +Kivy +---- + +.. code-block:: python + + from kivy.animation import Animation + from kivy.lang import Builder + from kivy.properties import NumericProperty + from kivy.uix.button import Button + from kivy.app import App + + + KV = ''' + Screen: + + ScaleButton: + size_hint: .5, .5 + pos_hint: {"center_x": .5, "center_y": .5} + on_release: app.change_scale(self) + + canvas.before: + PushMatrix + Scale: + x: self.scale_value_x + y: self.scale_value_y + z: self.scale_value_x + origin: self.center + canvas.after: + PopMatrix + ''' + + + class ScaleButton(Button): + scale_value_x = NumericProperty(1) + scale_value_y = NumericProperty(1) + scale_value_z = NumericProperty(1) + + + class Test(App): + def build(self): + return Builder.load_string(KV) + + def change_scale(self, instance_button: Button) -> None: + Animation( + scale_value_x=0.5, + scale_value_y=0.5, + scale_value_z=0.5, + d=0.3, + ).start(instance_button) + + + Test().run() + +KivyMD +------ + +.. code-block:: python + + from kivy.animation import Animation + from kivy.lang import Builder + from kivy.uix.behaviors import ButtonBehavior + + from kivymd.app import MDApp + from kivymd.uix.behaviors import ScaleBehavior + from kivymd.uix.boxlayout import MDBoxLayout + + KV = ''' + MDScreen: + + ScaleBox: + size_hint: .5, .5 + pos_hint: {"center_x": .5, "center_y": .5} + on_release: app.change_scale(self) + md_bg_color: "red" + ''' + + + class ScaleBox(ButtonBehavior, ScaleBehavior, MDBoxLayout): + pass + + + class Test(MDApp): + def build(self): + return Builder.load_string(KV) + + def change_scale(self, instance_button: ScaleBox) -> None: + Animation( + scale_value_x=0.5, + scale_value_y=0.5, + scale_value_z=0.5, + d=0.3, + ).start(instance_button) + + + Test().run() +""" + +__all__ = ("ScaleBehavior",) + +from kivy.lang import Builder +from kivy.properties import NumericProperty + +Builder.load_string( + """ + + canvas.before: + PushMatrix + Scale: + x: self.scale_value_x + y: self.scale_value_y + z: self.scale_value_x + origin: self.center + canvas.after: + PopMatrix +""" +) + + +class ScaleBehavior: + """Base class for controlling the scale of the widget.""" + + scale_value_x = NumericProperty(1) + """ + X-axis value. + + :attr:`scale_value_x` is an :class:`~kivy.properties.NumericProperty` + and defaults to `1`. + """ + + scale_value_y = NumericProperty(1) + """ + Y-axis value. + + :attr:`scale_value_y` is an :class:`~kivy.properties.NumericProperty` + and defaults to `1`. + """ + + scale_value_z = NumericProperty(1) + """ + Z-axis value. + + :attr:`scale_value_z` is an :class:`~kivy.properties.NumericProperty` + and defaults to `1`. + """ diff --git a/sbapp/kivymd/uix/behaviors/stencil_behavior.py b/sbapp/kivymd/uix/behaviors/stencil_behavior.py new file mode 100644 index 0000000..4bb5f6d --- /dev/null +++ b/sbapp/kivymd/uix/behaviors/stencil_behavior.py @@ -0,0 +1,134 @@ +""" +Behaviors/Stencil +================= + +.. versionadded:: 1.1.0 + +Base class for controlling the stencil instructions of the widget. + +.. note:: See `Stencil instructions + `_ + for more information. + +Kivy +---- + +.. code-block:: python + + from kivy.lang import Builder + from kivy.app import App + + KV = ''' + Carousel: + + Button: + size_hint: .9, .8 + pos_hint: {"center_x": .5, "center_y": .5} + + canvas.before: + StencilPush + RoundedRectangle: + pos: root.pos + size: root.size + StencilUse + canvas.after: + StencilUnUse + RoundedRectangle: + pos: root.pos + size: root.size + StencilPop + ''' + + + class Test(App): + def build(self): + return Builder.load_string(KV) + + + Test().run() + +KivyMD +------ + +.. code-block:: python + + from kivy.lang import Builder + + from kivymd.app import MDApp + from kivymd.uix.behaviors import StencilBehavior + from kivymd.uix.fitimage import FitImage + + KV = ''' + #:import os os + #:import images_path kivymd.images_path + + + MDCarousel: + + StencilImage: + size_hint: .9, .8 + pos_hint: {"center_x": .5, "center_y": .5} + source: os.path.join(images_path, "logo", "kivymd-icon-512.png") + ''' + + + class StencilImage(FitImage, StencilBehavior): + pass + + + class Test(MDApp): + def build(self): + return Builder.load_string(KV) + + + Test().run() +""" + +__all__ = ("StencilBehavior",) + +from kivy.lang import Builder +from kivy.properties import VariableListProperty + +Builder.load_string( + """ + + canvas.before: + StencilPush + RoundedRectangle: + pos: root.pos + size: root.size + # FIXME: Sometimes the radius has the value [], which get a + # `GraphicException: Invalid radius value, must be list of tuples/numerics` error + radius: root.radius if root.radius else [0, 0, 0, 0] + StencilUse + canvas.after: + StencilUnUse + RoundedRectangle: + pos: root.pos + size: root.size + # FIXME: Sometimes the radius has the value [], which get a + # `GraphicException: Invalid radius value, must be list of tuples/numerics` error + radius: root.radius if root.radius else [0, 0, 0, 0] + StencilPop +""" +) + + +class StencilBehavior: + """Base class for controlling the stencil instructions of the widget.""" + + radius = VariableListProperty([0], length=4) + """ + Canvas radius. + + .. versionadded:: 1.0.0 + + .. code-block:: python + + # Top left corner slice. + MDWidget: + radius: [25, 0, 0, 0] + + :attr:`radius` is an :class:`~kivy.properties.VariableListProperty` + and defaults to `[0, 0, 0, 0]`. + """ diff --git a/sbapp/kivymd/uix/behaviors/toggle_behavior.py b/sbapp/kivymd/uix/behaviors/toggle_behavior.py index 054849c..c396a29 100644 --- a/sbapp/kivymd/uix/behaviors/toggle_behavior.py +++ b/sbapp/kivymd/uix/behaviors/toggle_behavior.py @@ -14,61 +14,104 @@ example: pass -.. code-block:: python +.. tabs:: - from kivy.lang import Builder + .. tab:: Declarative KV style - from kivymd.app import MDApp - from kivymd.uix.behaviors.toggle_behavior import MDToggleButton - from kivymd.uix.button import MDRectangleFlatButton + .. code-block:: python - KV = ''' - Screen: + from kivy.lang import Builder - MDBoxLayout: - adaptive_size: True - pos_hint: {"center_x": .5, "center_y": .5} + from kivymd.app import MDApp + from kivymd.uix.behaviors.toggle_behavior import MDToggleButton + from kivymd.uix.button import MDFlatButton - MyToggleButton: - text: "Show ads" - group: "x" + KV = ''' + MDScreen: - MyToggleButton: - text: "Do not show ads" - group: "x" + MDBoxLayout: + adaptive_size: True + spacing: "12dp" + pos_hint: {"center_x": .5, "center_y": .5} - MyToggleButton: - text: "Does not matter" - group: "x" - ''' + MyToggleButton: + text: "Show ads" + group: "x" + + MyToggleButton: + text: "Do not show ads" + group: "x" + + MyToggleButton: + text: "Does not matter" + group: "x" + ''' - class MyToggleButton(MDRectangleFlatButton, MDToggleButton): - def __init__(self, **kwargs): - super().__init__(**kwargs) - self.background_down = self.theme_cls.primary_light + class MyToggleButton(MDFlatButton, MDToggleButton): + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + self.background_down = self.theme_cls.primary_color - class Test(MDApp): - def build(self): - return Builder.load_string(KV) + class Test(MDApp): + def build(self): + self.theme_cls.theme_style = "Dark" + self.theme_cls.primary_palette = "Orange" + return Builder.load_string(KV) - Test().run() + Test().run() + + .. tab:: Declarative python style + + .. code-block:: python + + from kivymd.app import MDApp + from kivymd.uix.behaviors.toggle_behavior import MDToggleButton + from kivymd.uix.boxlayout import MDBoxLayout + from kivymd.uix.button import MDFlatButton + from kivymd.uix.screen import MDScreen + + + class MyToggleButton(MDFlatButton, MDToggleButton): + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + self.background_down = self.theme_cls.primary_color + + + class Test(MDApp): + def build(self): + self.theme_cls.theme_style = "Dark" + self.theme_cls.primary_palette = "Orange" + return ( + MDScreen( + MDBoxLayout( + MyToggleButton( + text="Show ads", + group="x", + ), + MyToggleButton( + text="Do not show ads", + group="x", + ), + MyToggleButton( + text="Does not matter", + group="x", + ), + adaptive_size=True, + spacing="12dp", + pos_hint={"center_x": .5, "center_y": .5}, + ), + ) + ) + + + Test().run() .. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/toggle-button-1.gif :align: center -.. code-block:: python - - class MyToggleButton(MDFillRoundFlatButton, MDToggleButton): - def __init__(self, **kwargs): - self.background_down = MDApp.get_running_app().theme_cls.primary_dark - super().__init__(**kwargs) - -.. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/toggle-button-2.gif - :align: center - You can inherit the ``MyToggleButton`` class only from the following classes ---------------------------------------------------------------------------- @@ -88,6 +131,7 @@ from kivy.properties import BooleanProperty, ColorProperty from kivy.uix.behaviors import ToggleButtonBehavior from kivymd.uix.button import ( + ButtonContentsIconText, MDFillRoundFlatButton, MDFillRoundFlatIconButton, MDFlatButton, @@ -149,7 +193,8 @@ class MDToggleButton(ToggleButtonBehavior): # Do the object inherited from the "supported" buttons? if not issubclass(self.__class__, classinfo): raise ValueError( - f"Class {self.__class__} must be inherited from one of the classes in the list {classinfo}" + f"Class {self.__class__} must be inherited from one of the " + f"classes in the list {classinfo}" ) if ( not self.background_normal @@ -165,10 +210,12 @@ class MDToggleButton(ToggleButtonBehavior): ): self.__is_filled = True self.background_normal = self.theme_cls.primary_color - # If not the background_normal must be the same as the inherited one: + # If not background_normal must be the same as the inherited one. else: - self.background_normal = self.md_bg_color[:] - # If no background_down is setted: + self.background_normal = ( + self.md_bg_color[:] if self.md_bg_color else (0, 0, 0, 0) + ) + # If no background_down is setter. if ( not self.background_down ): # This means that if the value == [] or None will return True. @@ -200,3 +247,6 @@ class MDToggleButton(ToggleButtonBehavior): ): # If the background is transparent, the font color must be the # primary color. self.text_color = self.font_color_normal + + if issubclass(self.__class__, ButtonContentsIconText): + self.icon_color = self.text_color diff --git a/sbapp/kivymd/uix/bottomnavigation/bottomnavigation.kv b/sbapp/kivymd/uix/bottomnavigation/bottomnavigation.kv index 46c29f5..79f726b 100644 --- a/sbapp/kivymd/uix/bottomnavigation/bottomnavigation.kv +++ b/sbapp/kivymd/uix/bottomnavigation/bottomnavigation.kv @@ -1,4 +1,3 @@ -#:import sm kivy.uix.screenmanager #:import STANDARD_INCREMENT kivymd.material_resources.STANDARD_INCREMENT @@ -9,7 +8,7 @@ ScreenManager: id: tab_manager - transition: sm.FadeTransition(duration=.2) + transition: root.transition(duration=root.transition_duration) on_current: root.dispatch( \ "on_switch_tabs", \ @@ -96,7 +95,7 @@ radius: [16,] size: root._selected_region_width, dp(32) pos: - self.center_x - self.width - dp(8), \ + self.center_x - root._selected_region_width / 2, \ self.center_y - (dp(16)) MDLabel: diff --git a/sbapp/kivymd/uix/bottomnavigation/bottomnavigation.py b/sbapp/kivymd/uix/bottomnavigation/bottomnavigation.py index a3a77bc..d99f54b 100755 --- a/sbapp/kivymd/uix/bottomnavigation/bottomnavigation.py +++ b/sbapp/kivymd/uix/bottomnavigation/bottomnavigation.py @@ -62,59 +62,120 @@ For ease of understanding, this code works like this: Example ------- -.. code-block:: python +.. tabs:: - from kivy.lang import Builder + .. tab:: Declarative KV style - from kivymd.app import MDApp + .. code-block:: python + + from kivy.lang import Builder + + from kivymd.app import MDApp - class Test(MDApp): + class Test(MDApp): - def build(self): - self.theme_cls.material_style = "M3" - return Builder.load_string( - ''' - MDScreen: + def build(self): + self.theme_cls.material_style = "M3" + self.theme_cls.theme_style = "Dark" + return Builder.load_string( + ''' + MDScreen: - MDBottomNavigation: - panel_color: "#eeeaea" - selected_color_background: "#97ecf8" - text_color_active: 0, 0, 0, 1 + MDBottomNavigation: + #panel_color: "#eeeaea" + selected_color_background: "orange" + text_color_active: "lightgrey" - MDBottomNavigationItem: - name: 'screen 1' - text: 'Mail' - icon: 'gmail' - badge_icon: "numeric-10" + MDBottomNavigationItem: + name: 'screen 1' + text: 'Mail' + icon: 'gmail' + badge_icon: "numeric-10" - MDLabel: - text: 'Mail' - halign: 'center' + MDLabel: + text: 'Mail' + halign: 'center' - MDBottomNavigationItem: - name: 'screen 2' - text: 'Discord' - icon: 'discord' - badge_icon: "numeric-5" + MDBottomNavigationItem: + name: 'screen 2' + text: 'Twitter' + icon: 'twitter' + badge_icon: "numeric-5" - MDLabel: - text: 'Discord' - halign: 'center' + MDLabel: + text: 'Twitter' + halign: 'center' - MDBottomNavigationItem: - name: 'screen 3' - text: 'LinkedIN' - icon: 'linkedin' + MDBottomNavigationItem: + name: 'screen 3' + text: 'LinkedIN' + icon: 'linkedin' - MDLabel: - text: 'LinkedIN' - halign: 'center' - ''' - ) + MDLabel: + text: 'LinkedIN' + halign: 'center' + ''' + ) - Test().run() + Test().run() + + .. tab:: Declarative python style + + .. code-block:: python + + from kivymd.app import MDApp + from kivymd.uix.bottomnavigation import MDBottomNavigation, MDBottomNavigationItem + from kivymd.uix.label import MDLabel + from kivymd.uix.screen import MDScreen + + + class Test(MDApp): + def build(self): + self.theme_cls.material_style = "M3" + self.theme_cls.theme_style = "Dark" + return ( + MDScreen( + MDBottomNavigation( + MDBottomNavigationItem( + MDLabel( + text='Mail', + halign='center', + ), + name='screen 1', + text='Mail', + icon='gmail', + badge_icon="numeric-10", + ), + MDBottomNavigationItem( + MDLabel( + text='Twitter', + halign='center', + ), + name='screen 1', + text='Twitter', + icon='twitter', + badge_icon="numeric-10", + ), + MDBottomNavigationItem( + MDLabel( + text='LinkedIN', + halign='center', + ), + name='screen 1', + text='LinkedIN', + icon='linkedin', + badge_icon="numeric-10", + ), + selected_color_background="orange", + text_color_active="lightgrey", + ) + ) + ) + + + Test().run() .. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/bottom-navigation.gif :align: center @@ -192,13 +253,13 @@ from kivy.properties import ( ) from kivy.uix.behaviors import ButtonBehavior from kivy.uix.boxlayout import BoxLayout -from kivy.uix.screenmanager import ScreenManagerException +from kivy.uix.screenmanager import FadeTransition, ScreenManagerException from kivymd import uix_path from kivymd.material_resources import STANDARD_INCREMENT from kivymd.theming import ThemableBehavior, ThemeManager from kivymd.uix.anchorlayout import MDAnchorLayout -from kivymd.uix.behaviors import FakeRectangularElevationBehavior +from kivymd.uix.behaviors import CommonElevationBehavior, DeclarativeBehavior from kivymd.uix.behaviors.backgroundcolor_behavior import ( SpecificBackgroundColorBehavior, ) @@ -364,8 +425,8 @@ class MDTab(MDScreen, ThemableBehavior): and defaults to `''`. """ - def __init__(self, **kwargs): - super().__init__(**kwargs) + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) self.index = 0 self.parent_widget = None self.register_event_type("on_tab_touch_down") @@ -407,6 +468,31 @@ class MDBottomNavigationItem(MDTab): and defaults to `None`. """ + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + + def animate_header( + self, bottom_navigation_object, bottom_navigation_header_object + ) -> None: + if bottom_navigation_object.use_text: + Animation(_label_font_size=sp(12), d=0.1).start( + bottom_navigation_object.previous_tab.header + ) + Animation( + _selected_region_width=0, + t="in_out_sine", + d=0, + ).start(bottom_navigation_header_object) + Animation( + _text_color_normal=bottom_navigation_header_object.text_color_normal + if bottom_navigation_object.previous_tab.header.text_color_normal + != [1, 1, 1, 1] + else self.theme_cls.disabled_hint_text_color, + d=0.1, + ).start(bottom_navigation_object.previous_tab.header) + bottom_navigation_object.previous_tab.header.active = False + self.header.active = True + def on_tab_press(self, *args) -> None: """Called when clicking on a panel item.""" @@ -414,28 +500,13 @@ class MDBottomNavigationItem(MDTab): bottom_navigation_header_object = ( bottom_navigation_object.previous_tab.header ) - bottom_navigation_object.ids.tab_manager.current = self.name if bottom_navigation_object.previous_tab is not self: - if bottom_navigation_object.use_text: - Animation(_label_font_size=sp(12), d=0.1).start( - bottom_navigation_object.previous_tab.header - ) - Animation( - _selected_region_width=0, - t="in_out_sine", - d=0, - ).start(bottom_navigation_header_object) - Animation( - _text_color_normal=bottom_navigation_header_object.text_color_normal - if bottom_navigation_object.previous_tab.header.text_color_normal - != [1, 1, 1, 1] - else self.theme_cls.disabled_hint_text_color, - d=0.1, - ).start(bottom_navigation_object.previous_tab.header) - bottom_navigation_object.previous_tab.header.active = False - self.header.active = True - bottom_navigation_object.previous_tab = self + self.animate_header( + bottom_navigation_object, bottom_navigation_header_object + ) + + super().on_tab_press(*args) def on_disabled( self, instance_bottom_navigation_item, disabled_value: bool @@ -463,7 +534,7 @@ class TabbedPanelBase( and defaults to `None`. """ - previous_tab = ObjectProperty() + previous_tab = ObjectProperty(None, aloownone=True) """ :attr:`previous_tab` is an :class:`~MDTab` and defaults to `None`. """ @@ -479,7 +550,7 @@ class TabbedPanelBase( tabs = ListProperty() -class MDBottomNavigation(TabbedPanelBase): +class MDBottomNavigation(DeclarativeBehavior, TabbedPanelBase): """ A bottom navigation that is implemented by delegating all items to a :class:`~kivy.uix.screenmanager.ScreenManager`. @@ -492,6 +563,26 @@ class MDBottomNavigation(TabbedPanelBase): .. versionadded:: 1.0.0 """ + transition = ObjectProperty(FadeTransition) + """ + Transition animation of bottom navigation screen manager. + + .. versionadded:: 1.1.0 + + :attr:`transition` is an :class:`~kivy.properties.ObjectProperty` + and defaults to `FadeTransition`. + """ + + transition_duration = NumericProperty(0.2) + """ + Duration animation of bottom navigation screen manager. + + .. versionadded:: 1.1.0 + + :attr:`transition_duration` is an :class:`~kivy.properties.NumericProperty` + and defaults to `0.2`. + """ + text_color_normal = ColorProperty([1, 1, 1, 1]) """ Text color of the label when it is not selected. @@ -594,14 +685,15 @@ class MDBottomNavigation(TabbedPanelBase): and defaults to `False`. """ + widget_index = NumericProperty(0) + # Text active color if it is selected. _active_color = ColorProperty([1, 1, 1, 1]) - def __init__(self, **kwargs): - super().__init__(**kwargs) - self.register_event_type("on_switch_tabs") + def __init__(self, *args, **kwargs): self.previous_tab = None - self.widget_index = 0 + self.register_event_type("on_switch_tabs") + super().__init__(*args, **kwargs) self.theme_cls.bind(material_style=self.refresh_tabs) Window.bind(on_resize=self.on_resize) Clock.schedule_once(lambda x: self.on_resize()) @@ -660,8 +752,11 @@ class MDBottomNavigation(TabbedPanelBase): def on_selected_color_background( self, instance_bottom_navigation, color: list ) -> None: - for tab in self.ids.tab_bar.children: - tab.selected_color_background = color + def on_selected_color_background(*args): + for tab in self.ids.tab_bar.children: + tab.selected_color_background = color + + Clock.schedule_once(on_selected_color_background) def on_use_text( self, instance_bottom_navigation, use_text_value: bool @@ -698,12 +793,15 @@ class MDBottomNavigation(TabbedPanelBase): def on_text_color_active( self, instance_bottom_navigation, color: list ) -> None: - MDBottomNavigationHeader.text_color_active = color - self.text_color_active = color - for tab in self.ids.tab_bar.children: - tab.text_color_active = color - if tab.active: - tab._text_color_normal = color + def on_text_color_active(*args): + MDBottomNavigationHeader.text_color_active = color + self.text_color_active = color + for tab in self.ids.tab_bar.children: + tab.text_color_active = color + if tab.active: + tab._text_color_normal = color + + Clock.schedule_once(on_text_color_active) def on_switch_tabs(self, bottom_navigation_item, name_tab: str) -> None: """ @@ -759,8 +857,6 @@ class MDBottomNavigation(TabbedPanelBase): class MDBottomNavigationBar( - ThemableBehavior, - FakeRectangularElevationBehavior, - MDFloatLayout, + ThemableBehavior, CommonElevationBehavior, MDFloatLayout ): pass diff --git a/sbapp/kivymd/uix/bottomsheet/bottomsheet.py b/sbapp/kivymd/uix/bottomsheet/bottomsheet.py index bf7e98f..522f31b 100755 --- a/sbapp/kivymd/uix/bottomsheet/bottomsheet.py +++ b/sbapp/kivymd/uix/bottomsheet/bottomsheet.py @@ -34,7 +34,7 @@ Usage :class:`~MDListBottomSheet` MDTopAppBar: title: "Example BottomSheet" pos_hint: {"top": 1} - elevation: 10 + elevation: 4 MDRaisedButton: text: "Open list bottom sheet" @@ -94,7 +94,7 @@ which will be used as an icon to the left of the item: MDTopAppBar: title: 'Example BottomSheet' pos_hint: {"top": 1} - elevation: 10 + elevation: 4 MDRaisedButton: text: "Open grid bottom sheet" @@ -180,7 +180,7 @@ which will be used as an icon to the left of the item: MDTopAppBar: title: 'Example BottomSheet' pos_hint: {"top": 1} - elevation: 10 + elevation: 4 MDRaisedButton: text: "Open custom bottom sheet" diff --git a/sbapp/kivymd/uix/boxlayout.py b/sbapp/kivymd/uix/boxlayout.py index b5e0a52..6922de7 100644 --- a/sbapp/kivymd/uix/boxlayout.py +++ b/sbapp/kivymd/uix/boxlayout.py @@ -8,7 +8,7 @@ with some widget properties. For example: BoxLayout --------- -.. code-block:: +.. code-block:: kv BoxLayout: size_hint_y: None @@ -24,7 +24,7 @@ BoxLayout MDBoxLayout ----------- -.. code-block:: +.. code-block:: kv MDBoxLayout: adaptive_height: True @@ -88,7 +88,13 @@ __all__ = ("MDBoxLayout",) from kivy.uix.boxlayout import BoxLayout from kivymd.uix import MDAdaptiveWidget +from kivymd.uix.behaviors import DeclarativeBehavior -class MDBoxLayout(BoxLayout, MDAdaptiveWidget): - pass +class MDBoxLayout(DeclarativeBehavior, BoxLayout, MDAdaptiveWidget): + """ + Box layout class. + + For more information, see in the + :class:`~kivy.uix.boxlayout.BoxLayout` class documentation. + """ diff --git a/sbapp/kivymd/uix/button/__init__.py b/sbapp/kivymd/uix/button/__init__.py index dddf186..b833a4c 100644 --- a/sbapp/kivymd/uix/button/__init__.py +++ b/sbapp/kivymd/uix/button/__init__.py @@ -1,6 +1,7 @@ # NOQA F401 from .button import ( BaseButton, + ButtonContentsIconText, MDFillRoundFlatButton, MDFillRoundFlatIconButton, MDFlatButton, diff --git a/sbapp/kivymd/uix/button/button.kv b/sbapp/kivymd/uix/button/button.kv index c7523a4..4cee339 100644 --- a/sbapp/kivymd/uix/button/button.kv +++ b/sbapp/kivymd/uix/button/button.kv @@ -3,9 +3,9 @@ Clear Color: rgba: - (self._md_bg_color or [0.0, 0.0, 0.0, 0.0]) \ + self._md_bg_color \ if not self.disabled else \ - (self._md_bg_color_disabled or [0.0, 0.0, 0.0, 0.0]) + self._md_bg_color_disabled RoundedRectangle: size: self.size pos: self.pos @@ -13,19 +13,17 @@ radius: [root._radius, ] Color: rgba: - root._line_color or [0.0, 0.0, 0.0, 0.0] \ + root._line_color \ if not root.disabled else \ - ( \ - root._line_color_disabled \ - or self._disabled_color \ - or [0.0, 0.0, 0.0, 0.0] \ - ) + (root._line_color_disabled or self._disabled_color) Line: width: root.line_width rounded_rectangle: - (self.x, self.y, self.width, self.height, \ + ( \ + self.x, self.y, self.width, self.height, \ root._radius, root._radius, root._radius, root._radius, \ - self.height) + self.height \ + ) size_hint: None, None anchor_x: root.halign @@ -33,21 +31,28 @@ _round_rad: [self._radius] * 4 - lbl_txt: lbl_txt width: - max(root._min_width, \ - root.padding[0] + lbl_txt.texture_size[0] + root.padding[2]) + max( \ + root._min_width, \ + root.padding[0] + lbl_txt.texture_size[0] + root.padding[2] \ + ) size_hint_min_x: - max(root._min_width, \ - root.padding[0] + lbl_txt.texture_size[0] + root.padding[2]) + max( \ + root._min_width, \ + root.padding[0] + lbl_txt.texture_size[0] + root.padding[2] \ + ) height: - max(root._min_height, \ - root.padding[1] + lbl_txt.texture_size[1] + root.padding[3]) + max( \ + root._min_height, \ + root.padding[1] + lbl_txt.texture_size[1] + root.padding[3] \ + ) size_hint_min_y: - max(root._min_height, \ - root.padding[1] + lbl_txt.texture_size[1] + root.padding[3]) + max( \ + root._min_height, \ + root.padding[1] + lbl_txt.texture_size[1] + root.padding[3] \ + ) MDLabel: id: lbl_txt @@ -84,7 +89,10 @@ # This is only a temporary fix and does not fix the cause of the error. (root._icon_color if root._icon_color else root.theme_cls.text_color) \ if not root.disabled else \ - root.theme_cls.disabled_hint_text_color + root.theme_cls.disabled_hint_text_color \ + if not root.disabled_color else \ + root.disabled_color + on_icon: if self.icon not in md_icons.keys(): self.size_hint = (1, 1) theme_text_color: root._theme_icon_color @@ -131,7 +139,7 @@ id: box adaptive_size: True padding: 0 - spacing: "4dp" + spacing: "8dp" MDIcon: id: lbl_ic @@ -193,48 +201,21 @@ radius: [self.height / 2] - + theme_text_color: "Custom" md_bg_color: self.theme_cls.primary_color + + + padding_x: "8dp" + padding_y: "8dp" + adaptive_size: True + theme_text_color: "Custom" + canvas.before: - PushMatrix - Rotate: - angle: self._angle - axis: (0, 0, 1) - origin: self.center - canvas.after: - PopMatrix - - -# FIXME: Use :class:`~kivymd.uix.boxlayout.MDBoxLayout` instead -# :class:`~kivy.uix.boxlayout.BoxLayout`. - - size_hint: None, None - padding: "8dp", "4dp", "8dp", "4dp" - height: label.texture_size[1] + self.padding[1] * 2 - width: label.texture_size[0] + self.padding[0] * 2 - elevation: 10 - - # TODO: Use `md_bg_color` and `radius` instead `canvasю - canvas: Color: - rgba: - self.theme_cls.primary_color \ - if not root.bg_color else \ - root.bg_color + rgba: self.bg_color RoundedRectangle: - pos: self.pos size: self.size - radius: [5] - - Label: - id: label - markup: True - text: root.text - size_hint: None, None - size: self.texture_size - color: - root.theme_cls.text_color \ - if not root.text_color else \ - root.text_color + pos: self.pos + radius: self.radius diff --git a/sbapp/kivymd/uix/button/button.py b/sbapp/kivymd/uix/button/button.py index ddfa81e..3a96831 100755 --- a/sbapp/kivymd/uix/button/button.py +++ b/sbapp/kivymd/uix/button/button.py @@ -33,31 +33,62 @@ Components/Button MDIconButton ------------ -.. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/md-icon-button.gif +.. tabs:: + + .. tab:: Declarative KV style + + .. code-block:: python + + from kivy.lang import Builder + + from kivymd.app import MDApp + + KV = ''' + MDScreen: + + MDIconButton: + icon: "language-python" + pos_hint: {"center_x": .5, "center_y": .5} + ''' + + + class Example(MDApp): + def build(self): + self.theme_cls.theme_style = "Dark" + self.theme_cls.primary_palette = "Orange" + return Builder.load_string(KV) + + + Example().run() + + .. tab:: Declarative python style + + .. code-block:: python + + from kivymd.app import MDApp + from kivymd.uix.button import MDIconButton + from kivymd.uix.screen import MDScreen + + + class Example(MDApp): + def build(self): + self.theme_cls.theme_style = "Dark" + self.theme_cls.primary_palette = "Orange" + return ( + MDScreen( + MDIconButton( + icon="language-python", + pos_hint={"center_x": 0.5, "center_y": 0.5}, + ) + ) + ) + + + Example().run() + +.. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/md-icon-button.png :align: center -.. code-block:: python - - from kivy.lang import Builder - - from kivymd.app import MDApp - - KV = ''' - MDScreen: - - MDIconButton: - icon: "language-python" - pos_hint: {"center_x": .5, "center_y": .5} - ''' - - - class Example(MDApp): - def build(self): - return Builder.load_string(KV) - - - Example().run() - The :class:`~MDIconButton.icon` parameter must have the name of the icon from ``kivymd/icon_definitions.py`` file. @@ -66,9 +97,9 @@ You can also use custom icons: .. code-block:: kv MDIconButton: - icon: "data/logo/kivy-icon-256.png" + icon: "kivymd/images/logo/kivymd-icon-256.png" -.. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/md-icon-custom-button.gif +.. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/md-icon-custom-button.png :align: center By default, :class:`~MDIconButton` button has a size ``(dp(48), dp (48))``. @@ -80,7 +111,7 @@ Use :class:`~BaseButton.icon_size` attribute to resize the button: icon: "android" icon_size: "64sp" -.. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/md-icon-button-user-font-size.gif +.. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/md-icon-button-user-font-size.png :align: center By default, the color of :class:`~MDIconButton` @@ -88,8 +119,6 @@ By default, the color of :class:`~MDIconButton` You can change the color of :class:`~MDIconButton` as the text color of :class:`~kivymd.uix.label.MDLabel`, substituting ``theme_icon_color`` for ``theme_text_color`` and ``icon_color`` for ``text_color``. -The use of ``user_font_size``, ``text_color`` and ``theme_text_color`` for -:class:`~MDIconButton` is deprecated. .. code-block:: kv @@ -145,8 +174,10 @@ Material design style 3 ''' - class TestNavigationDrawer(MDApp): + class Example(MDApp): def build(self): + self.theme_cls.theme_style = "Dark" + self.theme_cls.primary_palette = "Orange" self.theme_cls.material_style = "M3" return Builder.load_string(KV) @@ -168,26 +199,23 @@ Material design style 3 ) - TestNavigationDrawer().run() + Example().run() -.. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/md-floating-action-button-m3.gif +.. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/md-floating-action-button-m3.png :align: center .. MDFlatButton: MDFlatButton ------------ -.. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/md-flat-button.gif - :align: center - To change the text color of: class:`~MDFlatButton` use the ``text_color`` parameter: .. code-block:: kv MDFlatButton: - text: "MDFLATBUTTON" + text: "MDFlatButton" theme_text_color: "Custom" - text_color: 0, 0, 1, 1 + text_color: "orange" .. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/md-flat-button-text-color.png :align: center @@ -197,7 +225,7 @@ Or use markup: .. code-block:: kv MDFlatButton: - text: "[color=#00ffcc]MDFLATBUTTON[/color]" + text: "[color=#00ffcc]MDFlatButton[/color]" To specify the font size and font name, use the parameters as in the usual `Kivy` buttons: @@ -205,7 +233,7 @@ To specify the font size and font name, use the parameters as in the usual .. code-block:: kv MDFlatButton: - text: "MDFLATBUTTON" + text: "MDFlatButton" font_size: "18sp" font_name: "path/to/font" @@ -213,33 +241,29 @@ To specify the font size and font name, use the parameters as in the usual MDRaisedButton -------------- -.. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/md-raised-button.gif - :align: center - This button is similar to the :class:`~MDFlatButton` button except that you can set the background color for :class:`~MDRaisedButton`: .. code-block:: kv MDRaisedButton: - text: "MDRAISEDBUTTON" - md_bg_color: 1, 0, 1, 1 + text: "MDRaisedButton" + md_bg_color: "red" +.. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/md-raised-button.png + :align: center .. MDRectangleFlatButton: MDRectangleFlatButton --------------------- -.. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/md-rectangle-flat-button.gif - :align: center - .. code-block:: kv MDRectangleFlatButton: - text: "MDRECTANGLEFLATBUTTON" + text: "MDRectangleFlatButton" theme_text_color: "Custom" - text_color: 1, 0, 0, 1 - line_color: 0, 0, 1, 1 + text_color: "white" + line_color: "red" .. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/md-rectangle-flat-button-md-bg-color.png :align: center @@ -248,9 +272,6 @@ MDRectangleFlatButton MDRectangleFlatIconButton ------------------------- -.. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/md-rectangle-flat-icon-button.png - :align: center - Button parameters :class:`~MDRectangleFlatIconButton` are the same as button :class:`~MDRectangleFlatButton`, with the addition of the ``theme_icon_color`` and ``icon_color`` parameters as for :class:`~MDIconButton`. @@ -259,12 +280,12 @@ button :class:`~MDRectangleFlatButton`, with the addition of the MDRectangleFlatIconButton: icon: "android" - text: "MDRECTANGLEFLATICONBUTTON" + text: "MDRectangleFlatIconButton" theme_text_color: "Custom" - text_color: 0, 0, 1, 1 - line_color: 1, 0, 1, 1 + text_color: "white" + line_color: "red" theme_icon_color: "Custom" - icon_color: 1, 0, 0, 1 + icon_color: "orange" .. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/md-rectangle-flat-icon-button-custom.png :align: center @@ -281,16 +302,18 @@ Without border class Example(MDApp): def build(self): - screen = MDScreen() - screen.add_widget( - MDRectangleFlatIconButton( - text="MDRectangleFlatIconButton", - icon="language-python", - line_color=(0, 0, 0, 0), - pos_hint={"center_x": .5, "center_y": .5}, + self.theme_cls.theme_style = "Dark" + self.theme_cls.primary_palette = "Orange" + return ( + MDScreen( + MDRectangleFlatIconButton( + text="MDRectangleFlatIconButton", + icon="language-python", + line_color=(0, 0, 0, 0), + pos_hint={"center_x": .5, "center_y": .5}, + ) ) ) - return screen Example().run() @@ -303,6 +326,9 @@ Without border line_color: 0, 0, 0, 0 pos_hint: {"center_x": .5, "center_y": .5} +.. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/md-rectangle-flat-icon-button-without-border.png + :align: center + .. MDRoundFlatButton: MDRoundFlatButton ----------------- @@ -310,8 +336,8 @@ MDRoundFlatButton .. code-block:: kv MDRoundFlatButton: - text: "MDROUNDFLATBUTTON" - text_color: 0, 1, 0, 1 + text: "MDRoundFlatButton" + text_color: "white" .. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/md-round-flat-button-text-color.png :align: center @@ -320,9 +346,6 @@ MDRoundFlatButton MDRoundFlatIconButton --------------------- -.. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/md-round-flat-icon-button.png - :align: center - Button parameters :class:`~MDRoundFlatIconButton` are the same as button :class:`~MDRoundFlatButton`, with the addition of the ``theme_icon_color`` and ``icon_color`` parameters as for :class:`~MDIconButton`: @@ -330,8 +353,12 @@ button :class:`~MDRoundFlatButton`, with the addition of the .. code-block:: kv MDRoundFlatIconButton: + text: "MDRoundFlatIconButton" icon: "android" - text: "MDROUNDFLATICONBUTTON" + text_color: "white" + +.. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/md-round-flat-icon-button.png + :align: center .. MDFillRoundFlatButton: MDFillRoundFlatButton @@ -361,14 +388,14 @@ button :class:`~MDRaisedButton`, with the addition of the MDTextButton ------------ -.. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/md-text-button.png - :align: center - .. code-block:: kv MDTextButton: - text: "MDTEXTBUTTON" - custom_color: 0, 1, 0, 1 + text: "MDTextButton" + custom_color: "white" + +.. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/md-text-button.png + :align: center .. MDFloatingActionButtonSpeedDial: MDFloatingActionButtonSpeedDial @@ -400,6 +427,8 @@ MDFloatingActionButtonSpeedDial } def build(self): + self.theme_cls.theme_style = "Dark" + self.theme_cls.primary_palette = "Orange" return Builder.load_string(KV) @@ -410,30 +439,65 @@ MDFloatingActionButtonSpeedDial Or without KV Language: -.. code-block:: python +.. tabs:: - from kivymd.uix.screen import MDScreen - from kivymd.app import MDApp - from kivymd.uix.button import MDFloatingActionButtonSpeedDial + .. tab:: Imperative python style + + .. code-block:: python + + from kivymd.uix.screen import MDScreen + from kivymd.app import MDApp + from kivymd.uix.button import MDFloatingActionButtonSpeedDial - class Example(MDApp): - data = { - 'Python': 'language-python', - 'PHP': 'language-php', - 'C++': 'language-cpp', - } + class Example(MDApp): + data = { + 'Python': 'language-python', + 'PHP': 'language-php', + 'C++': 'language-cpp', + } - def build(self): - screen = MDScreen() - speed_dial = MDFloatingActionButtonSpeedDial() - speed_dial.data = self.data - speed_dial.root_button_anim = True - screen.add_widget(speed_dial) - return screen + def build(self): + self.theme_cls.theme_style = "Dark" + self.theme_cls.primary_palette = "Orange" + screen = MDScreen() + speed_dial = MDFloatingActionButtonSpeedDial() + speed_dial.data = self.data + speed_dial.root_button_anim = True + screen.add_widget(speed_dial) + return screen - Example().run() + Example().run() + + .. tab:: Declarative python style + + .. code-block:: python + + from kivymd.uix.screen import MDScreen + from kivymd.app import MDApp + from kivymd.uix.button import MDFloatingActionButtonSpeedDial + + + class Example(MDApp): + def build(self): + self.theme_cls.theme_style = "Dark" + self.theme_cls.primary_palette = "Orange" + return ( + MDScreen( + MDFloatingActionButtonSpeedDial( + data={ + 'Python': 'language-python', + 'PHP': 'language-php', + 'C++': 'language-cpp', + }, + root_button_anim=True, + ) + ) + ) + + + Example().run() You can use various types of animation of labels for buttons on the stack: @@ -445,21 +509,133 @@ You can use various types of animation of labels for buttons on the stack: .. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/MDFloatingActionButtonSpeedDial-hint.gif :align: center -You can set your color values ​​for background, text of buttons etc: +You can set your color values for background, text of buttons etc: .. code-block:: kv MDFloatingActionButtonSpeedDial: - bg_hint_color: app.theme_cls.primary_light + hint_animation: True + bg_hint_color: app.theme_cls.primary_dark .. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/MDFloatingActionButtonSpeedDial-hint-color.png :align: center -.. seealso:: +Binds to individual buttons +--------------------------- - `See full example `_ +.. tabs:: + + .. tab:: Declarative KV style + + .. code-block:: python + + from kivy.lang import Builder + from kivy.properties import DictProperty + + from kivymd.app import MDApp + + KV = ''' + MDScreen: + + MDFloatingActionButtonSpeedDial: + id: speed_dial + data: app.data + root_button_anim: True + hint_animation: True + ''' + + + class Example(MDApp): + data = DictProperty() + + def build(self): + self.theme_cls.theme_style = "Dark" + self.theme_cls.primary_palette = "Orange" + self.data = { + 'Python': 'language-python', + 'JS': [ + 'language-javascript', + "on_press", lambda x: print("pressed JS"), + "on_release", lambda x: print( + "stack_buttons", + self.root.ids.speed_dial.stack_buttons + ) + ], + 'PHP': [ + 'language-php', + "on_press", lambda x: print("pressed PHP"), + "on_release", self.callback + ], + 'C++': [ + 'language-cpp', + "on_press", lambda x: print("pressed C++"), + "on_release", lambda x: self.callback() + ], + } + return Builder.load_string(KV) + + def callback(self, *args): + print(args) + + + Example().run() + + .. tab:: Declarative python style + + .. code-block:: python + + from kivymd.app import MDApp + from kivymd.uix.button import MDFloatingActionButtonSpeedDial + from kivymd.uix.screen import MDScreen + + + class Example(MDApp): + def build(self): + self.theme_cls.theme_style = "Dark" + self.theme_cls.primary_palette = "Orange" + return ( + MDScreen( + MDFloatingActionButtonSpeedDial( + id="speed_dial", + hint_animation=True, + root_button_anim=True, + ) + ) + ) + + def on_start(self): + data = { + "Python": "language-python", + "JS": [ + "language-javascript", + "on_press", lambda x: print("pressed JS"), + "on_release", lambda x: print( + "stack_buttons", + self.root.ids.speed_dial.stack_buttons + ) + ], + "PHP": [ + "language-php", + "on_press", lambda x: print("pressed PHP"), + "on_release", self.callback + ], + "C++": [ + "language-cpp", + "on_press", lambda x: print("pressed C++"), + "on_release", lambda x: self.callback() + ], + } + self.root.ids.speed_dial.data = data + + def callback(self, *args): + print(args) + + + Example().run() """ +from __future__ import annotations + __all__ = ( "BaseButton", "MDIconButton", @@ -497,8 +673,8 @@ from kivy.properties import ( ) from kivy.uix.anchorlayout import AnchorLayout from kivy.uix.behaviors import ButtonBehavior -from kivy.uix.boxlayout import BoxLayout from kivy.uix.floatlayout import FloatLayout +from kivy.weakproxy import WeakProxy from kivymd import uix_path from kivymd.color_definitions import text_colors @@ -506,9 +682,9 @@ from kivymd.font_definitions import theme_font_styles from kivymd.theming import ThemableBehavior from kivymd.uix.behaviors import ( CommonElevationBehavior, - FakeRectangularElevationBehavior, + DeclarativeBehavior, RectangularRippleBehavior, - RoundedRectangularElevationBehavior, + RotateBehavior, ) from kivymd.uix.label import MDLabel from kivymd.uix.tooltip import MDTooltip @@ -528,11 +704,76 @@ theme_text_color_options = ( "ContrastParentBackground", ) +# FIXME: If you set a new elevation value for the button +# (press the "Set elevation" button), then disable the button +# (press the "Disabled" button), and then enable the button +# (press the "Undisabled" button), then the previously set elevation value is +# reset to zero. +# In addition, if you set a new elevation value +# (press the "Set elevation" button) and click on the button for which we set +# the elevation value, then the new elevation value will receive the previous +# elevation value. This problem is only related to the buttons. +# For example, there is no such problem for the MDCard widget. + +""" +from kivy.lang import Builder + +from kivymd.app import MDApp + +KV = ''' +MDScreen: + + MDRaisedButton: + size_hint: .5, .5 + id: button + pos_hint: {"center_x": .5, "center_y": .5} + elevation: 0 + + MDBoxLayout: + adaptive_size: True + pos_hint: {"center_x": .5} + spacing: 12 + padding: 12 + + MDRaisedButton: + text: "Set elevation" + pos_hint: {"center_x": .5, "bottom": 1} + on_release: button.elevation = 4 + + MDRaisedButton: + text: "Disabled" + pos_hint: {"center_x": .5, "bottom": 1} + on_release: button.disabled = True + + MDRaisedButton: + text: "Undisabled" + pos_hint: {"center_x": .5, "bottom": 1} + on_release: button.disabled = False +''' + + +class Test(MDApp): + def build(self): + return Builder.load_string(KV) + + +Test().run() +""" + class BaseButton( - RectangularRippleBehavior, ThemableBehavior, ButtonBehavior, AnchorLayout + DeclarativeBehavior, + RectangularRippleBehavior, + ThemableBehavior, + ButtonBehavior, + AnchorLayout, ): - """Base class for all buttons.""" + """ + Base class for all buttons. + + For more information, see in the + :class:`~kivy.uix.anchorlayout.AnchorLayout` class documentation. + """ padding = VariableListProperty([dp(16), dp(8), dp(16), dp(8)]) """ @@ -618,7 +859,7 @@ class BaseButton( text_color = ColorProperty(None) """ - Button text color in (r, g, b, a) format. + Button text color in (r, g, b, a) or string format. :attr:`text_color` is a :class:`~kivy.properties.ColorProperty` and defaults to `None`. @@ -626,7 +867,7 @@ class BaseButton( icon_color = ColorProperty(None) """ - Button icon color in (r, g, b, a) format. + Button icon color in (r, g, b, a) or string format. :attr:`icon_color` is a :class:`~kivy.properties.ColorProperty` and defaults to `None`. @@ -659,18 +900,6 @@ class BaseButton( and defaults to `None`. """ - user_font_size = NumericProperty(0, deprecated=True) - """ - Custom font size for :class:`~MDIconButton`. - - .. deprecated in 1.0.0:: - - Use :attr:`icon_size` instead. - - :attr:`user_font_size` is a :class:`~kivy.properties.NumericProperty` - and defaults to `0`. - """ - line_width = NumericProperty(1) """ Line width for button border. @@ -681,7 +910,7 @@ class BaseButton( line_color = ColorProperty(None) """ - Line color for button border. + Line color in (r, g, b, a) or string format for button border. :attr:`line_color` is a :class:`~kivy.properties.ColorProperty` and defaults to `None`. @@ -689,7 +918,7 @@ class BaseButton( line_color_disabled = ColorProperty(None) """ - Disabled line color for button border. + Disabled line color in (r, g, b, a) or string format for button border. .. versionadded:: 1.0.0 @@ -699,7 +928,7 @@ class BaseButton( md_bg_color = ColorProperty(None) """ - Button background color. + Button background color in (r, g, b, a) or string format. :attr:`md_bg_color` is a :class:`~kivy.properties.ColorProperty` and defaults to `None`. @@ -707,7 +936,8 @@ class BaseButton( md_bg_color_disabled = ColorProperty(None) """ - The background color of the button when the button is disabled. + The background color in (r, g, b, a) or string format of the button when + the button is disabled. :attr:`md_bg_color_disabled` is a :class:`~kivy.properties.ColorProperty` and defaults to `None`. @@ -715,8 +945,8 @@ class BaseButton( disabled_color = ColorProperty(None) """ - The color of the text and icon when the button is disabled, in the - (r, g, b, a) format. + The color of the text and icon when the button is disabled, + in (r, g, b, a) or string format. .. versionadded:: 1.0.0 @@ -737,11 +967,11 @@ class BaseButton( # Note - _radius must be > 0 to avoid rendering issues. _radius = BoundedNumericProperty(dp(4), min=0.0999, errorvalue=0.1) # Properties used for rendering. - _disabled_color = ColorProperty(None) - _md_bg_color = ColorProperty(None) - _md_bg_color_disabled = ColorProperty(None) - _line_color = ColorProperty(None) - _line_color_disabled = ColorProperty(None) + _disabled_color = ColorProperty([0.0, 0.0, 0.0, 0.0]) + _md_bg_color = ColorProperty([0.0, 0.0, 0.0, 0.0]) + _md_bg_color_disabled = ColorProperty([0.0, 0.0, 0.0, 0.0]) + _line_color = ColorProperty([0.0, 0.0, 0.0, 0.0]) + _line_color_disabled = ColorProperty([0.0, 0.0, 0.0, 0.0]) _theme_text_color = OptionProperty(None, options=theme_text_color_options) _theme_icon_color = OptionProperty(None, options=theme_text_color_options) _text_color = ColorProperty(None) @@ -763,8 +993,8 @@ class BaseButton( _animation_fade_bg = ObjectProperty(None, allownone=True) - def __init__(self, **kwargs): - super().__init__(**kwargs) + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) self.theme_cls.bind( primary_palette=self.set_all_colors, theme_style=self.set_all_colors, @@ -818,14 +1048,14 @@ class BaseButton( """Set all button colours (except text/icons).""" # Set main color - self._md_bg_color = ( + _md_bg_color = ( self.md_bg_color or self._default_md_bg_color or self.theme_cls.primary_color ) # Set disabled color - self._md_bg_color_disabled = ( + _md_bg_color_disabled = ( self.md_bg_color_disabled or ( [sum(self.md_bg_color[0:3]) / 3.0] * 3 @@ -838,14 +1068,14 @@ class BaseButton( ) # Set line color - self._line_color = ( + _line_color = ( self.line_color or self._default_line_color or self.theme_cls.primary_color ) # Set disabled line color - self._line_color_disabled = ( + _line_color_disabled = ( self.line_color_disabled or ( [sum(self.line_color[0:3]) / 3.0] * 3 @@ -857,6 +1087,21 @@ class BaseButton( or self.theme_cls.disabled_primary_color ) + if self.theme_cls.theme_style_switch_animation: + Animation( + _md_bg_color=_md_bg_color, + _md_bg_color_disabled=_md_bg_color_disabled, + _line_color=_line_color, + _line_color_disabled=_line_color_disabled, + d=self.theme_cls.theme_style_switch_animation_duration, + t="linear", + ).start(self) + else: + self._md_bg_color = _md_bg_color + self._md_bg_color_disabled = _md_bg_color_disabled + self._line_color = _line_color + self._line_color_disabled = _line_color_disabled + def set_text_color(self, *args) -> None: """ Set _theme_text_color and _text_color based on defaults and options. @@ -881,7 +1126,9 @@ class BaseButton( """ self._theme_icon_color = ( - self.theme_icon_color or self._default_theme_icon_color + (self.theme_icon_color or self._default_theme_icon_color) + if not self.disabled + else "Custom" ) if self._default_icon_color == "PrimaryHue": default_icon_color = text_colors[self.theme_cls.primary_palette][ @@ -940,6 +1187,10 @@ class BaseButton( return super().on_touch_up(touch) def on_disabled(self, instance_button, disabled_value: bool) -> None: + if hasattr(super(), "on_disabled"): + if self.disabled is True: + Animation.cancel_all(self, "elevation") + super().on_disabled(instance_button, disabled_value) Clock.schedule_once(self.set_disabled_color) @@ -957,30 +1208,20 @@ class ButtonElevationBehaviour(CommonElevationBehavior): _elevation_raised = NumericProperty() _anim_raised = ObjectProperty(None, allownone=True) - _default_elevation = 2 + _default_elevation = 3 def __init__(self, **kwargs): + super().__init__(**kwargs) if self.elevation == 0: self.elevation = self._default_elevation - super().__init__(**kwargs) - self.bind(_radius=self.setter("radius")) - self.on_elevation(self, self.elevation) - - def on_elevation(self, instance_button, elevation_value: int) -> None: - super().on_elevation(instance_button, elevation_value) - self._elevation_raised = self.elevation + 6 + if hasattr(self, "radius"): + self.bind(_radius=self.setter("radius")) + Clock.schedule_once(self.create_anim_raised) self.on_disabled(self, self.disabled) - def on__elevation_raised( - self, instance_button, elevation_value: int - ) -> None: - Animation.cancel_all(self, "_elevation") - self._anim_raised = Animation(_elevation=self._elevation_raised, d=0.15) - - def on_disabled(self, instance_button, disabled_value: bool) -> None: - if self.disabled is True: - Animation.cancel_all(self, "_elevation") - super().on_disabled(instance_button, disabled_value) + def create_anim_raised(self, *args) -> None: + self._elevation_raised = self.elevation + 1.2 + self._anim_raised = Animation(elevation=self.elevation + 1, d=0.15) def on_touch_down(self, touch): if not self.disabled: @@ -1003,8 +1244,8 @@ class ButtonElevationBehaviour(CommonElevationBehavior): return super().on_touch_up(touch) def stop_elevation_anim(self): - Animation.cancel_all(self, "_elevation") - self._elevation = self.elevation + Animation.cancel_all(self, "elevation") + self.elevation = self._elevation_raised - 1 class ButtonContentsText: @@ -1018,12 +1259,6 @@ class ButtonContentsIcon: _min_width = NumericProperty(0) - def __init__(self, **kwargs): - super().__init__(**kwargs) - if self.user_font_size: - self.icon_size = self.user_font_size - self.bind(user_font_size=self.setter("icon_size")) - def on_text_color(self, instance_button, color: list) -> None: """ Set icon_color equal to text_color. @@ -1074,7 +1309,7 @@ class OldButtonIconMixin: self.theme_icon_color = "Custom" -class MDFlatButton(ButtonContentsText, BaseButton): +class MDFlatButton(BaseButton, ButtonContentsText): """ A flat rectangular button with (by default) no border or background. Text is the default text color. @@ -1095,12 +1330,7 @@ class MDFlatButton(ButtonContentsText, BaseButton): """ -class MDRaisedButton( - FakeRectangularElevationBehavior, - ButtonElevationBehaviour, - ButtonContentsText, - BaseButton, -): +class MDRaisedButton(BaseButton, ButtonElevationBehaviour, ButtonContentsText): """ A flat button with (by default) a primary color fill and matching color text. @@ -1113,8 +1343,14 @@ class MDRaisedButton( _default_theme_text_color = "Custom" _default_text_color = "PrimaryHue" + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + self.shadow_softness = 8 + self.shadow_offset = (0, 2) + self.shadow_radius = self._radius * 2 -class MDRectangleFlatButton(ButtonContentsText, BaseButton): + +class MDRectangleFlatButton(BaseButton, ButtonContentsText): """ A flat button with (by default) a primary color border and primary color text. @@ -1127,7 +1363,7 @@ class MDRectangleFlatButton(ButtonContentsText, BaseButton): class MDRectangleFlatIconButton( - OldButtonIconMixin, ButtonContentsIconText, BaseButton + BaseButton, OldButtonIconMixin, ButtonContentsIconText ): """ A flat button with (by default) a primary color border, primary color text @@ -1142,7 +1378,7 @@ class MDRectangleFlatIconButton( _default_icon_color = "Primary" -class MDRoundFlatButton(ButtonContentsText, BaseButton): +class MDRoundFlatButton(BaseButton, ButtonContentsText): """ A flat button with (by default) fully rounded corners, a primary color border and primary color text. @@ -1153,15 +1389,13 @@ class MDRoundFlatButton(ButtonContentsText, BaseButton): _default_theme_text_color = "Custom" _default_text_color = "Primary" - def __init__(self, **kwargs): - super().__init__(**kwargs) + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) self.rounded_button = True class MDRoundFlatIconButton( - OldButtonIconMixin, - ButtonContentsIconText, - BaseButton, + BaseButton, OldButtonIconMixin, ButtonContentsIconText ): """ A flat button with (by default) rounded corners, a primary color border, @@ -1175,12 +1409,12 @@ class MDRoundFlatIconButton( _default_text_color = "Primary" _default_icon_color = "Primary" - def __init__(self, **kwargs): - super().__init__(**kwargs) + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) self.rounded_button = True -class MDFillRoundFlatButton(ButtonContentsText, BaseButton): +class MDFillRoundFlatButton(BaseButton, ButtonContentsText): """ A flat button with (by default) rounded corners, a primary color fill and primary color text. @@ -1191,15 +1425,13 @@ class MDFillRoundFlatButton(ButtonContentsText, BaseButton): _default_theme_text_color = "Custom" _default_text_color = "PrimaryHue" - def __init__(self, **kwargs): - super().__init__(**kwargs) + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) self.rounded_button = True class MDFillRoundFlatIconButton( - OldButtonIconMixin, - ButtonContentsIconText, - BaseButton, + BaseButton, OldButtonIconMixin, ButtonContentsIconText ): """ A flat button with (by default) rounded corners, a primary color fill, @@ -1213,12 +1445,12 @@ class MDFillRoundFlatIconButton( _default_text_color = "PrimaryHue" _default_icon_color = "PrimaryHue" - def __init__(self, **kwargs): - super().__init__(**kwargs) + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) self.rounded_button = True -class MDIconButton(OldButtonIconMixin, ButtonContentsIcon, BaseButton): +class MDIconButton(BaseButton, OldButtonIconMixin, ButtonContentsIcon): """A simple rounded icon button.""" icon = StringProperty("checkbox-blank-circle") @@ -1229,39 +1461,11 @@ class MDIconButton(OldButtonIconMixin, ButtonContentsIcon, BaseButton): and defaults to `'checkbox-blank-circle'`. """ - text_color = ColorProperty(None, deprecated=True) - """ - Button icon color in (r, g, b, a) format. - - .. deprecated in 1.0.0:: - - Deprecated for :class:`~MDIconButton`. Use ``icon_color`` instead. - - :attr:`text_color` is a :class:`~kivy.properties.ColorProperty` - and defaults to `None`. - """ - - theme_text_color = OptionProperty( - None, options=theme_text_color_options, deprecated=True - ) - """ - Button icon type. Available options are: (`"Primary"`, `"Secondary"`, - `"Hint"`, `"Error"`, `"Custom"`, `"ContrastParentBackground"`). - - .. deprecated in 1.0.0:: - - Deprecated for :class:`~MDIconButton`. Use ``theme_icon_color`` instead. - - - :attr:`theme_text_color` is an :class:`~kivy.properties.OptionProperty` - and defaults to `None` (set by button class). - """ - _min_width = NumericProperty(0) _default_icon_pad = max(dp(48) - sp(24), 0) - def __init__(self, **kwargs): - super().__init__(**kwargs) + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) self.rounded_button = True # FIXME: GraphicException: Invalid width value, must be > 0 self.line_width = 0.001 @@ -1279,11 +1483,7 @@ class MDIconButton(OldButtonIconMixin, ButtonContentsIcon, BaseButton): class MDFloatingActionButton( - OldButtonIconMixin, - RoundedRectangularElevationBehavior, - ButtonElevationBehaviour, - ButtonContentsIcon, - BaseButton, + BaseButton, OldButtonIconMixin, ButtonElevationBehaviour, ButtonContentsIcon ): """ Implementation @@ -1311,12 +1511,11 @@ class MDFloatingActionButton( _default_theme_icon_color = "Custom" _default_icon_color = "PrimaryHue" - def __init__(self, **kwargs): - super().__init__(**kwargs) + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) # FIXME: GraphicException: Invalid width value, must be > 0 self.line_width = 0.001 - self.theme_cls.bind(material_style=self.set_size) - self.theme_cls.bind(material_style=self.set__radius) + self.theme_cls.bind(material_style=self.set_size_and_radius) Clock.schedule_once(self.set_size) Clock.schedule_once(self.set__radius) Clock.schedule_once(self.set_font_size) @@ -1330,9 +1529,13 @@ class MDFloatingActionButton( def set__radius(self, *args) -> None: if self.theme_cls.material_style == "M2": + self.shadow_radius = self.height / 2 self.rounded_button = True else: + self.shadow_softness = 8 + self.shadow_offset = (0, 2) self.rounded_button = False + if self.type == "small": self._radius = dp(12) elif self.type == "standard": @@ -1340,6 +1543,12 @@ class MDFloatingActionButton( elif self.type == "large": self._radius = dp(28) + self.shadow_radius = self._radius + + def set_size_and_radius(self, *args) -> None: + self.set_size(args) + self.set__radius(args) + def set_size(self, *args) -> None: if self.theme_cls.material_style == "M2": self.size = dp(56), dp(56) @@ -1359,7 +1568,7 @@ class MDFloatingActionButton( class MDTextButton(ButtonBehavior, MDLabel): color = ColorProperty(None) """ - Button color in (r, g, b, a) format. + Button color in (r, g, b, a) or string format. :attr:`color` is a :class:`~kivy.properties.ColorProperty` and defaults to `None`. @@ -1367,7 +1576,7 @@ class MDTextButton(ButtonBehavior, MDLabel): color_disabled = ColorProperty(None) """ - Button color disabled in (r, g, b, a) format. + Button color disabled in (r, g, b, a) or string format. :attr:`color_disabled` is a :class:`~kivy.properties.ColorProperty` and defaults to `None`. @@ -1400,14 +1609,6 @@ class MDTextButton(ButtonBehavior, MDLabel): # SpeedDial classes -class BaseFloatingRootButton(MDFloatingActionButton): - _angle = NumericProperty(0) - - def __init__(self, **kwargs): - super().__init__(**kwargs) - self.elevation = 5 - - class BaseFloatingBottomButton(MDFloatingActionButton, MDTooltip): _canvas_width = NumericProperty(0) _padding_right = NumericProperty(0) @@ -1418,41 +1619,48 @@ class BaseFloatingBottomButton(MDFloatingActionButton, MDTooltip): self.height = "46dp" -# FIXME: Use :class:`~kivymd.uix.boxlayout.MDBoxLayout` instead -# :class:`~kivy.uix.boxlayout.BoxLayout`. -class BaseFloatingLabel( - ThemableBehavior, FakeRectangularElevationBehavior, BoxLayout -): - text = StringProperty() - text_color = ColorProperty(None) - bg_color = ColorProperty(None) - - class MDFloatingBottomButton(BaseFloatingBottomButton): - pass + _bg_color = ColorProperty(None) -class MDFloatingRootButton(BaseFloatingRootButton): - pass +class MDFloatingRootButton(RotateBehavior, MDFloatingActionButton): + rotate_value_angle = NumericProperty(0) -class MDFloatingLabel(BaseFloatingLabel): - pass +class MDFloatingLabel(MDLabel): + bg_color = ColorProperty([0, 0, 0, 0]) -class MDFloatingActionButtonSpeedDial(ThemableBehavior, FloatLayout): +class MDFloatingActionButtonSpeedDial( + DeclarativeBehavior, ThemableBehavior, FloatLayout +): """ + For more information, see in the + :class:`~kivy.uix.floatlayout.FloatLayout` class documentation. + :Events: :attr:`on_open` Called when a stack is opened. :attr:`on_close` Called when a stack is closed. + :attr:`on_press_stack_button` + Called at the on_press event for the stack button. + :attr:`on_release_stack_button` + Called at the on_press event for the stack button. """ icon = StringProperty("plus") """ Root button icon name. + .. code-block:: kv + + MDFloatingActionButtonSpeedDial: + icon: "pencil" + + .. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/MDFloatingActionButtonSpeedDial-icon.png + :align: center + :attr:`icon` is a :class:`~kivy.properties.StringProperty` and defaults to `'plus'`. """ @@ -1465,36 +1673,59 @@ class MDFloatingActionButtonSpeedDial(ThemableBehavior, FloatLayout): and defaults to `'right'`. """ - callback = ObjectProperty(lambda x: None) + label_text_color = ColorProperty(None) """ - Custom callback. + Color of floating text labels in (r, g, b, a) or string format. .. code-block:: kv MDFloatingActionButtonSpeedDial: - callback: app.callback + label_text_color: "orange" - .. code-block:: python + .. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/MDFloatingActionButtonSpeedDial-label-text-color.png + :align: center - def callback(self, instance): - print(instance.icon) - - - :attr:`callback` is a :class:`~kivy.properties.ObjectProperty` + :attr:`label_text_color` is a :class:`~kivy.properties.ColorProperty` and defaults to `None`. """ - label_text_color = ColorProperty([0, 0, 0, 1]) + label_bg_color = ColorProperty([0, 0, 0, 0]) """ - Floating text color in (r, g, b, a) format. + Background color of floating text labels in (r, g, b, a) or string format. - :attr:`label_text_color` is a :class:`~kivy.properties.ColorProperty` - and defaults to `[0, 0, 0, 1]`. + .. code-block:: kv + + MDFloatingActionButtonSpeedDial: + label_text_color: "black" + label_bg_color: "orange" + + .. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/MDFloatingActionButtonSpeedDial-label-bg-color.png + :align: center + + :attr:`label_bg_color` is a :class:`~kivy.properties.ColorProperty` + and defaults to `[0, 0, 0, 0]`. + """ + + label_radius = VariableListProperty([0], length=4) + """ + The radius of the background of floating text labels. + + .. code-block:: kv + + MDFloatingActionButtonSpeedDial: + label_text_color: "black" + label_bg_color: "orange" + + .. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/MDFloatingActionButtonSpeedDial-label-radius.png + :align: center + + :attr:`label_radius` is a :class:`~kivy.properties.ColorProperty` + and defaults to `[0, 0, 0, 0]`. """ data = DictProperty() """ - Must be a dictionary + Must be a dictionary. .. code-block:: python @@ -1505,18 +1736,33 @@ class MDFloatingActionButtonSpeedDial(ThemableBehavior, FloatLayout): } """ - right_pad = BooleanProperty(True) + right_pad = BooleanProperty(False) """ - If `True`, the button will increase on the right side by 2.5 pixels - if the :attr:`~hint_animation` parameter equal to `True`. + If `True`, the background for the floating text label will increase by the + number of pixels specified in the :attr:`~right_pad_value` parameter. + + Works only if the :attr:`~hint_animation` parameter is set to `True`. .. rubric:: False + .. code-block:: kv + + MDFloatingActionButtonSpeedDial: + hint_animation: True + right_pad: False + .. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/MDFloatingActionButtonSpeedDial-right-pad.gif :align: center .. rubric:: True + .. code-block:: kv + + MDFloatingActionButtonSpeedDial: + hint_animation: True + right_pad: True + right_pad_value: "10dp" + .. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/MDFloatingActionButtonSpeedDial-right-pad-true.gif :align: center @@ -1524,6 +1770,14 @@ class MDFloatingActionButtonSpeedDial(ThemableBehavior, FloatLayout): and defaults to `False`. """ + right_pad_value = NumericProperty(0) + """ + See :attr:`~right_pad` parameter for more information. + + :attr:`right_pad_value` is a :class:`~kivy.properties.NumericProperty` + and defaults to `0`. + """ + root_button_anim = BooleanProperty(False) """ If ``True`` then the root button will rotate 45 degrees when the stack @@ -1612,39 +1866,87 @@ class MDFloatingActionButtonSpeedDial(ThemableBehavior, FloatLayout): bg_color_root_button = ColorProperty(None) """ - Root button color in (r, g, b, a) format. + Background color of root button in (r, g, b, a) or string format. + + .. code-clock:: kv + + MDFloatingActionButtonSpeedDial: + bg_color_root_button: "red" + + .. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/MDFloatingActionButtonSpeedDial-bg-color-root-button.png + :align: center :attr:`bg_color_root_button` is a :class:`~kivy.properties.ColorProperty` - and defaults to `[]`. + and defaults to `None`. """ bg_color_stack_button = ColorProperty(None) """ - The color of the buttons in the stack (r, g, b, a) format. + Background color of the stack buttons in (r, g, b, a) or string format. + + .. code-clock:: kv + + MDFloatingActionButtonSpeedDial: + bg_color_root_button: "red" + bg_color_stack_button: "red" + + .. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/MDFloatingActionButtonSpeedDial-bg-color-stack-button.png + :align: center :attr:`bg_color_stack_button` is a :class:`~kivy.properties.ColorProperty` - and defaults to `[]`. + and defaults to `None`. """ color_icon_stack_button = ColorProperty(None) """ - The color icon of the buttons in the stack (r, g, b, a) format. + The color icon of the stack buttons in (r, g, b, a) or string format. + + .. code-clock:: kv + + MDFloatingActionButtonSpeedDial: + bg_color_root_button: "red" + bg_color_stack_button: "red" + color_icon_stack_button: "white" + + .. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/MDFloatingActionButtonSpeedDial-color-icon-stack-button.png + :align: center :attr:`color_icon_stack_button` is a :class:`~kivy.properties.ColorProperty` - and defaults to `[]`. + and defaults to `None`. """ color_icon_root_button = ColorProperty(None) """ - The color icon of the root button (r, g, b, a) format. + The color icon of the root button in (r, g, b, a) or string format. + + .. code-clock:: kv + + MDFloatingActionButtonSpeedDial: + bg_color_root_button: "red" + bg_color_stack_button: "red" + color_icon_stack_button: "white" + color_icon_root_button: self.color_icon_stack_button + + .. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/MDFloatingActionButtonSpeedDial-color-icon-root-button.png + :align: center :attr:`color_icon_root_button` is a :class:`~kivy.properties.ColorProperty` - and defaults to `[]`. + and defaults to `None`. """ bg_hint_color = ColorProperty(None) """ - Background color for the text of the buttons in the stack (r, g, b, a) format. + Background color for the floating text of the buttons in (r, g, b, a) + or string format. + + .. code-clock:: kv + + MDFloatingActionButtonSpeedDial: + bg_hint_color: "red" + hint_animation: True + + .. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/MDFloatingActionButtonSpeedDial-bg-hint-color.png + :align: center :attr:`bg_hint_color` is a :class:`~kivy.properties.ColorProperty` and defaults to `None`. @@ -1652,12 +1954,14 @@ class MDFloatingActionButtonSpeedDial(ThemableBehavior, FloatLayout): hint_animation = BooleanProperty(False) """ - Whether to use button extension animation to display text labels. + Whether to use button extension animation to display floating text. :attr:`hint_animation` is a :class:`~kivy.properties.BooleanProperty` and defaults to `False`. """ + stack_buttons = DictProperty() + _label_pos_y_set = False _anim_buttons_data = {} _anim_labels_data = {} @@ -1666,6 +1970,8 @@ class MDFloatingActionButtonSpeedDial(ThemableBehavior, FloatLayout): super().__init__(**kwargs) self.register_event_type("on_open") self.register_event_type("on_close") + self.register_event_type("on_press_stack_button") + self.register_event_type("on_release_stack_button") Window.bind(on_resize=self._update_pos_buttons) def on_open(self, *args): @@ -1700,19 +2006,22 @@ class MDFloatingActionButtonSpeedDial(ThemableBehavior, FloatLayout): if self.state == "open": for widget in self.children: if isinstance(widget, MDFloatingLabel) and self.hint_animation: - widget._elevation = 0 Animation.cancel_all(widget) for item in self.data.items(): if widget.text in item: Animation( _canvas_width=widget.width + dp(24), - _padding_right=dp(5) if self.right_pad else 0, + _padding_right=self.right_pad_value + if self.right_pad + else 0, d=self.opening_time, t=self.opening_transition, ).start(instance_button) if ( instance_button.icon == self.data[f"{widget.text}"] + or instance_button.icon + == self.data[f"{widget.text}"][0] ): Animation( opacity=1, @@ -1727,51 +2036,68 @@ class MDFloatingActionButtonSpeedDial(ThemableBehavior, FloatLayout): def on_data(self, instance_speed_dial, data: dict) -> None: """Creates a stack of buttons.""" - # FIXME: Don't know how to fix AttributeError error: - # File "kivymd/uix/button.py", line 1597, in on_data - # self.add_widget(bottom_button) - # File "kivy/uix/floatlayout.py", line 140, in add_widget - # return super(FloatLayout, self).add_widget(widget, index, canvas) - # File "kivy/uix/layout.py", line 97, in add_widget - # return super(Layout, self).add_widget(widget, index, canvas) - # File "kivy/uix/widget.py", line 629, in add_widget - # canvas.add(widget.canvas) - # AttributeError: 'NoneType' object has no attribute 'add' - super().__init__() + def on_data(*args): + # Bottom buttons. + for name, parameters in data.items(): + name_icon = ( + parameters if (type(parameters) is str) else parameters[0] + ) + + bottom_button = MDFloatingBottomButton( + icon=name_icon, + on_enter=self.on_enter, + on_leave=self.on_leave, + opacity=0, + ) + bottom_button.bind( + on_press=lambda x: self.dispatch("on_press_stack_button"), + on_release=lambda x: self.dispatch( + "on_release_stack_button" + ), + ) + + if "on_press" in parameters: + callback = parameters[parameters.index("on_press") + 1] + bottom_button.bind(on_press=callback) + + if "on_release" in parameters: + callback = parameters[parameters.index("on_release") + 1] + bottom_button.bind(on_release=callback) + + self.set_pos_bottom_buttons(bottom_button) + self.add_widget(bottom_button) + self.stack_buttons[name] = WeakProxy(bottom_button) + # Labels. + floating_text = name + if floating_text: + label = MDFloatingLabel(text=floating_text, opacity=0) + label.bg_color = self.label_bg_color + label.radius = self.label_radius + label.text_color = ( + self.label_text_color + if self.label_text_color + else self.theme_cls.text_color + ) + self.add_widget(label) + # Top root button. + root_button = MDFloatingRootButton(on_release=self.open_stack) + root_button.icon = self.icon + self.set_pos_root_button(root_button) + self.add_widget(root_button) + self.clear_widgets() + self.stack_buttons = {} self._anim_buttons_data = {} self._anim_labels_data = {} self._label_pos_y_set = False - - # Bottom buttons. - for name, name_icon in data.items(): - bottom_button = MDFloatingBottomButton( - icon=name_icon, - on_enter=self.on_enter, - on_leave=self.on_leave, - opacity=0, - ) - bottom_button.bind( - on_release=lambda x=bottom_button: self.callback(x) - ) - self.set_pos_bottom_buttons(bottom_button) - self.add_widget(bottom_button) - # Labels. - floating_text = name - if floating_text: - label = MDFloatingLabel(text=floating_text, opacity=0) - label.text_color = self.label_text_color - self.add_widget(label) - # Top root button. - root_button = MDFloatingRootButton(on_release=self.open_stack) - root_button.icon = self.icon - self.set_pos_root_button(root_button) - self.add_widget(root_button) + Clock.schedule_once(on_data) def on_icon(self, instance_speed_dial, name_icon: str) -> None: - self._get_count_widget(MDFloatingRootButton).icon = name_icon + self._set_button_property(MDFloatingRootButton, "icon", name_icon) - def on_label_text_color(self, instance_speed_dial, color: list) -> None: + def on_label_text_color( + self, instance_speed_dial, color: list | str + ) -> None: for widget in self.children: if isinstance(widget, MDFloatingLabel): widget.text_color = color @@ -1779,34 +2105,52 @@ class MDFloatingActionButtonSpeedDial(ThemableBehavior, FloatLayout): def on_color_icon_stack_button( self, instance_speed_dial, color: list ) -> None: - for widget in self.children: - if isinstance(widget, MDFloatingBottomButton): - widget.text_color = color + self._set_button_property(MDFloatingBottomButton, "icon_color", color) def on_hint_animation(self, instance_speed_dial, value: bool) -> None: for widget in self.children: if isinstance(widget, MDFloatingLabel): - widget.bg_color = (0, 0, 0, 0) + widget.md_bg_color = (0, 0, 0, 0) def on_bg_hint_color(self, instance_speed_dial, color: list) -> None: - for widget in self.children: - if isinstance(widget, MDFloatingBottomButton): - widget._bg_color = color + setattr(MDFloatingBottomButton, "_bg_color", color) def on_color_icon_root_button( self, instance_speed_dial, color: list ) -> None: - self._get_count_widget(MDFloatingRootButton).text_color = color + self._set_button_property(MDFloatingRootButton, "icon_color", color) def on_bg_color_stack_button( self, instance_speed_dial, color: list ) -> None: - for widget in self.children: - if isinstance(widget, MDFloatingBottomButton): - widget.md_bg_color = color + self._set_button_property(MDFloatingBottomButton, "md_bg_color", color) def on_bg_color_root_button(self, instance_speed_dial, color: list) -> None: - self._get_count_widget(MDFloatingRootButton).md_bg_color = color + self._set_button_property(MDFloatingRootButton, "md_bg_color", color) + + def on_press_stack_button(self, *args) -> None: + """ + Called at the on_press event for the stack button. + + .. code-block:: kv + + MDFloatingActionButtonSpeedDial: + on_press_stack_button: print(*args) + + .. versionadded:: 1.1.0 + """ + + def on_release_stack_button(self, *args) -> None: + """ + Called at the on_release event for the stack button. + + .. code-block:: kv + + MDFloatingActionButtonSpeedDial: + on_release_stack_button: print(*args) + + .. versionadded:: 1.1.0 + """ def set_pos_labels(self, instance_floating_label: MDFloatingLabel) -> None: """ @@ -1860,7 +2204,7 @@ class MDFloatingActionButtonSpeedDial(ThemableBehavior, FloatLayout): if self.state != "open": y = 0 - label_position = dp(56) + label_position = dp(54) anim_buttons_data = {} anim_labels_data = {} @@ -1892,7 +2236,7 @@ class MDFloatingActionButtonSpeedDial(ThemableBehavior, FloatLayout): ): # Rotates the root button 45 degrees. Animation( - _angle=-45, + rotate_value_angle=-45, d=self.opening_time_button_rotation, t=self.opening_transition_button_rotation, ).start(widget) @@ -1951,13 +2295,14 @@ class MDFloatingActionButtonSpeedDial(ThemableBehavior, FloatLayout): opacity=0, ).start(widget) elif isinstance(widget, MDFloatingLabel): - Animation(opacity=0, d=0.1).start(widget) + if widget.opacity > 0: + Animation(opacity=0, d=0.1).start(widget) elif ( isinstance(widget, MDFloatingRootButton) and self.root_button_anim ): Animation( - _angle=0, + rotate_value_angle=0, d=self.closing_time_button_rotation, t=self.closing_transition_button_rotation, ).start(widget) @@ -1974,9 +2319,15 @@ class MDFloatingActionButtonSpeedDial(ThemableBehavior, FloatLayout): elif isinstance(widget, MDFloatingLabel): self.set_pos_labels(widget) - def _get_count_widget(self, instance): - widget = None - for widget in self.children: - if isinstance(widget, instance): - break - return widget + def _set_button_property( + self, instance, property_name: str, property_value: str | list + ): + def set_count_widget(*args): + if self.children: + for widget in self.children: + if isinstance(widget, instance): + setattr(instance, property_name, property_value) + Clock.unschedule(set_count_widget) + break + + Clock.schedule_interval(set_count_widget, 0) diff --git a/sbapp/kivymd/uix/card/card.kv b/sbapp/kivymd/uix/card/card.kv index ac67a51..7ed37bd 100644 --- a/sbapp/kivymd/uix/card/card.kv +++ b/sbapp/kivymd/uix/card/card.kv @@ -2,17 +2,6 @@ md_bg_color: app.theme_cls.divider_color - - canvas.before: - Color: - rgba: self.md_bg_color - RoundedRectangle: - size: self.size - pos: self.pos - radius: root.radius - source: root.background - - md_bg_color: self.theme_cls.divider_color \ diff --git a/sbapp/kivymd/uix/card/card.py b/sbapp/kivymd/uix/card/card.py index 85f7f27..d224ae1 100755 --- a/sbapp/kivymd/uix/card/card.py +++ b/sbapp/kivymd/uix/card/card.py @@ -26,97 +26,141 @@ Components/Card MDCard ------ -.. warning:: Starting from the KivyMD 1.0.0 library version, it is necessary - to manually inherit the card class from one of the ``Elevation`` classes - from ``kivymd/uix/behaviors/elevation.py`` module to draw the card shadow. - -.. code-block:: python - - from kivymd.uix.behaviors import RoundedRectangularElevationBehavior - from kivymd.uix.card import MDCard - - - class MD3Card(MDCard, RoundedRectangularElevationBehavior): - '''Implements a material design v3 card.''' - -This may sound awkward to you, but it actually allows for better control over -the providers that implement the rendering of the shadows. - -.. note:: You can read more information about the classes that implement the - rendering of shadows on this `documentation page `_. - An example of the implementation of a card in the style of material design version 3 ------------------------------------------------------------------------------------ -.. code-block:: python +.. tabs:: - from kivy.lang import Builder - from kivy.properties import StringProperty + .. tab:: Declarative KV and imperative python styles - from kivymd.app import MDApp - from kivymd.uix.behaviors import RoundedRectangularElevationBehavior - from kivymd.uix.card import MDCard + .. code-block:: python - KV = ''' - - padding: 16 - size_hint: None, None - size: "200dp", "100dp" + from kivy.lang import Builder + from kivy.properties import StringProperty - MDRelativeLayout: - size_hint: None, None - size: root.size + from kivymd.app import MDApp + from kivymd.uix.card import MDCard - MDIconButton: - icon: "dots-vertical" - pos: - root.width - (self.width + root.padding[0] + dp(4)), \ - root.height - (self.height + root.padding[0] + dp(4)) + KV = ''' + + padding: 4 + size_hint: None, None + size: "200dp", "100dp" - MDLabel: - id: label - text: root.text - adaptive_size: True - color: .2, .2, .2, .8 + MDRelativeLayout: + + MDIconButton: + icon: "dots-vertical" + pos_hint: {"top": 1, "right": 1} + + MDLabel: + id: label + text: root.text + adaptive_size: True + color: "grey" + pos: "12dp", "12dp" + bold: True - MDScreen: + MDScreen: - MDBoxLayout: - id: box - adaptive_size: True - spacing: "56dp" - pos_hint: {"center_x": .5, "center_y": .5} - ''' + MDBoxLayout: + id: box + adaptive_size: True + spacing: "56dp" + pos_hint: {"center_x": .5, "center_y": .5} + ''' - class MD3Card(MDCard, RoundedRectangularElevationBehavior): - '''Implements a material design v3 card.''' + class MD3Card(MDCard): + '''Implements a material design v3 card.''' - text = StringProperty() + text = StringProperty() - class TestCard(MDApp): - def build(self): - self.theme_cls.material_style = "M3" - return Builder.load_string(KV) + class Example(MDApp): + def build(self): + self.theme_cls.material_style = "M3" + return Builder.load_string(KV) - def on_start(self): - styles = { - "elevated": "#f6eeee", "filled": "#f4dedc", "outlined": "#f8f5f4" - } - for style in styles.keys(): - self.root.ids.box.add_widget( - MD3Card( - line_color=(0.2, 0.2, 0.2, 0.8), - style=style, - text=style.capitalize(), - md_bg_color=styles[style], + def on_start(self): + styles = { + "elevated": "#f6eeee", "filled": "#f4dedc", "outlined": "#f8f5f4" + } + for style in styles.keys(): + self.root.ids.box.add_widget( + MD3Card( + line_color=(0.2, 0.2, 0.2, 0.8), + style=style, + text=style.capitalize(), + md_bg_color=styles[style], + ) + ) + + + Example().run() + + .. tab:: Declarative python styles + + .. code-block:: python + + from kivymd.app import MDApp + from kivymd.uix.boxlayout import MDBoxLayout + from kivymd.uix.button import MDIconButton + from kivymd.uix.card import MDCard + from kivymd.uix.label import MDLabel + from kivymd.uix.relativelayout import MDRelativeLayout + from kivymd.uix.screen import MDScreen + + + class MD3Card(MDCard): + '''Implements a material design v3 card.''' + + + class Example(MDApp): + def build(self): + self.theme_cls.material_style = "M3" + return ( + MDScreen( + MDBoxLayout( + id="box", + adaptive_size=True, + spacing="56dp", + pos_hint={"center_x": 0.5, "center_y": 0.5}, + ) + ) ) - ) + + def on_start(self): + styles = { + "elevated": "#f6eeee", "filled": "#f4dedc", "outlined": "#f8f5f4" + } + for style in styles.keys(): + self.root.ids.box.add_widget( + MD3Card( + MDRelativeLayout( + MDIconButton( + icon="dots-vertical", + pos_hint={"top": 1, "right": 1} + ), + MDLabel( + text=style.capitalize(), + adaptive_size=True, + color="grey", + pos=("12dp", "12dp"), + ), + ), + line_color=(0.2, 0.2, 0.2, 0.8), + style=style, + padding="4dp", + size_hint=(None, None), + size=("200dp", "100dp"), + md_bg_color=styles[style], + ) + ) - TestCard().run() + Example().run() .. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/cards-m3.png :align: center @@ -125,16 +169,13 @@ An example of the implementation of a card in the style of material design versi MDCardSwipe ----------- -.. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/MDCardSwipe.gif - :align: center - To create a card with `swipe-to-delete` behavior, you must create a new class that inherits from the :class:`~MDCardSwipe` class: .. code-block:: kv - : + size_hint_y: None height: content.height @@ -152,80 +193,142 @@ that inherits from the :class:`~MDCardSwipe` class: class SwipeToDeleteItem(MDCardSwipe): text = StringProperty() -.. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/map-mdcard-swipr.png +.. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/sceleton-mdcard-swiper.png :align: center End full code ------------- -.. code-block:: python +.. tabs:: - from kivy.lang import Builder - from kivy.properties import StringProperty + .. tab:: Declarative KV and imperative python styles - from kivymd.app import MDApp - from kivymd.uix.card import MDCardSwipe + .. code-block:: python - KV = ''' - : - size_hint_y: None - height: content.height + from kivy.lang import Builder + from kivy.properties import StringProperty - MDCardSwipeLayerBox: - # Content under the card. + from kivymd.app import MDApp + from kivymd.uix.card import MDCardSwipe - MDCardSwipeFrontBox: + KV = ''' + + size_hint_y: None + height: content.height - # Content of card. - OneLineListItem: - id: content - text: root.text - _no_ripple_effect: True + MDCardSwipeLayerBox: + # Content under the card. + + MDCardSwipeFrontBox: + + # Content of card. + OneLineListItem: + id: content + text: root.text + _no_ripple_effect: True - MDScreen: + MDScreen: - MDBoxLayout: - orientation: "vertical" - spacing: "10dp" + MDBoxLayout: + orientation: "vertical" - MDTopAppBar: - elevation: 10 - title: "MDCardSwipe" + MDTopAppBar: + elevation: 4 + title: "MDCardSwipe" - ScrollView: - scroll_timeout : 100 + MDScrollView: + scroll_timeout : 100 - MDList: - id: md_list - padding: 0 - ''' + MDList: + id: md_list + padding: 0 + ''' - class SwipeToDeleteItem(MDCardSwipe): - '''Card with `swipe-to-delete` behavior.''' + class SwipeToDeleteItem(MDCardSwipe): + '''Card with `swipe-to-delete` behavior.''' - text = StringProperty() + text = StringProperty() - class TestCard(MDApp): - def __init__(self, **kwargs): - super().__init__(**kwargs) - self.screen = Builder.load_string(KV) + class Example(MDApp): + def build(self): + self.theme_cls.theme_style = "Dark" + self.theme_cls.primary_palette = "Orange" + return Builder.load_string(KV) - def build(self): - return self.screen + def on_start(self): + '''Creates a list of cards.''' - def on_start(self): - '''Creates a list of cards.''' - - for i in range(20): - self.screen.ids.md_list.add_widget( - SwipeToDeleteItem(text=f"One-line item {i}") - ) + for i in range(20): + self.root.ids.md_list.add_widget( + SwipeToDeleteItem(text=f"One-line item {i}") + ) - TestCard().run() + Example().run() + + .. tab:: Declarative python styles + + .. code-block:: python + + from kivymd.app import MDApp + from kivymd.uix.boxlayout import MDBoxLayout + from kivymd.uix.card import ( + MDCardSwipe, MDCardSwipeLayerBox, MDCardSwipeFrontBox + ) + from kivymd.uix.list import MDList, OneLineListItem + from kivymd.uix.screen import MDScreen + from kivymd.uix.scrollview import MDScrollView + from kivymd.uix.toolbar import MDTopAppBar + + + class Example(MDApp): + def build(self): + self.theme_cls.theme_style = "Dark" + self.theme_cls.primary_palette = "Orange" + return ( + MDScreen( + MDBoxLayout( + MDTopAppBar( + elevation=4, + title="MDCardSwipe", + ), + MDScrollView( + MDList( + id="md_list", + ), + id="scroll", + scroll_timeout=100, + ), + id="box", + orientation="vertical", + ), + ) + ) + + def on_start(self): + '''Creates a list of cards.''' + + for i in range(20): + self.root.ids.box.ids.scroll.ids.md_list.add_widget( + MDCardSwipe( + MDCardSwipeLayerBox(), + MDCardSwipeFrontBox( + OneLineListItem( + id="content", + text=f"One-line item {i}", + _no_ripple_effect=True, + ) + ), + size_hint_y=None, + height="52dp", + ) + ) + + + Example().run() .. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/list-mdcard-swipe.gif :align: center @@ -235,7 +338,7 @@ Binding a swipe to one of the sides of the screen .. code-block:: kv - : + # By default, the parameter is "left" anchor: "right" @@ -250,7 +353,7 @@ Swipe behavior .. code-block:: kv - : + # By default, the parameter is "hand" type_swipe: "hand" @@ -271,85 +374,41 @@ Removing an item using the ``type_swipe = "auto"`` parameter The map provides the :attr:`MDCardSwipe.on_swipe_complete` event. You can use this event to remove items from a list: -.. code-block:: kv +.. tabs:: - : - on_swipe_complete: app.on_swipe_complete(root) + .. tab:: Declarative KV styles -.. code-block:: python + .. code-block:: kv - def on_swipe_complete(self, instance): - self.screen.ids.md_list.remove_widget(instance) + : + on_swipe_complete: app.on_swipe_complete(root) -End full code -------------- + .. tab:: Declarative python styles -.. code-block:: python + .. code-block:: kv - from kivy.lang import Builder - from kivy.properties import StringProperty + .. code-block:: python - from kivymd.app import MDApp - from kivymd.uix.card import MDCardSwipe - - KV = ''' - : - size_hint_y: None - height: content.height - type_swipe: "auto" - on_swipe_complete: app.on_swipe_complete(root) - - MDCardSwipeLayerBox: - - MDCardSwipeFrontBox: - - OneLineListItem: - id: content - text: root.text - _no_ripple_effect: True - - - MDScreen: - - MDBoxLayout: - orientation: "vertical" - spacing: "10dp" - - MDTopAppBar: - elevation: 10 - title: "MDCardSwipe" - - ScrollView: - - MDList: - id: md_list - padding: 0 - ''' - - - class SwipeToDeleteItem(MDCardSwipe): - text = StringProperty() - - - class TestCard(MDApp): - def __init__(self, **kwargs): - super().__init__(**kwargs) - self.screen = Builder.load_string(KV) - - def build(self): - return self.screen - - def on_swipe_complete(self, instance): - self.screen.ids.md_list.remove_widget(instance) - - def on_start(self): - for i in range(20): - self.screen.ids.md_list.add_widget( - SwipeToDeleteItem(text=f"One-line item {i}") + MDCardSwipe( + ... + on_swipe_complete=self.on_swipe_complete, ) +.. tabs:: - TestCard().run() + .. tab:: Imperative python styles + + .. code-block:: python + + def on_swipe_complete(self, instance): + self.root.ids.md_list.remove_widget(instance) + + .. tab:: Decralative python styles + + .. code-block:: python + + def on_swipe_complete(self, instance): + self.root.ids.box.ids.scroll.ids.md_list.remove_widget(instance) .. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/autodelete-mdcard-swipe.gif :align: center @@ -375,76 +434,154 @@ use the :class:`~MDCardSwipeLayerBox` class. End full code ------------- -.. code-block:: python +.. tabs:: - from kivy.lang import Builder - from kivy.properties import StringProperty + .. tab:: Declarative KV styles - from kivymd.app import MDApp - from kivymd.uix.card import MDCardSwipe + .. code-block:: python - KV = ''' - : - size_hint_y: None - height: content.height + from kivy.lang import Builder + from kivy.properties import StringProperty - MDCardSwipeLayerBox: - padding: "8dp" + from kivymd.app import MDApp + from kivymd.uix.card import MDCardSwipe - MDIconButton: - icon: "trash-can" - pos_hint: {"center_y": .5} - on_release: app.remove_item(root) + KV = ''' + : + size_hint_y: None + height: content.height - MDCardSwipeFrontBox: + MDCardSwipeLayerBox: + padding: "8dp" - OneLineListItem: - id: content - text: root.text - _no_ripple_effect: True + MDIconButton: + icon: "trash-can" + pos_hint: {"center_y": .5} + on_release: app.remove_item(root) + + MDCardSwipeFrontBox: + + OneLineListItem: + id: content + text: root.text + _no_ripple_effect: True - MDScreen: + MDScreen: - MDBoxLayout: - orientation: "vertical" - spacing: "10dp" + MDBoxLayout: + orientation: "vertical" - MDTopAppBar: - elevation: 10 - title: "MDCardSwipe" + MDTopAppBar: + elevation: 4 + title: "MDCardSwipe" - ScrollView: + MDScrollView: - MDList: - id: md_list - padding: 0 - ''' + MDList: + id: md_list + padding: 0 + ''' - class SwipeToDeleteItem(MDCardSwipe): - text = StringProperty() + class SwipeToDeleteItem(MDCardSwipe): + text = StringProperty() - class TestCard(MDApp): - def __init__(self, **kwargs): - super().__init__(**kwargs) - self.screen = Builder.load_string(KV) + class Example(MDApp): + def __init__(self, **kwargs): + super().__init__(**kwargs) + self.theme_cls.theme_style = "Dark" + self.theme_cls.primary_palette = "Orange" + self.screen = Builder.load_string(KV) - def build(self): - return self.screen + def build(self): + return self.screen - def remove_item(self, instance): - self.screen.ids.md_list.remove_widget(instance) + def remove_item(self, instance): + self.screen.ids.md_list.remove_widget(instance) - def on_start(self): - for i in range(20): - self.screen.ids.md_list.add_widget( - SwipeToDeleteItem(text=f"One-line item {i}") - ) + def on_start(self): + for i in range(20): + self.screen.ids.md_list.add_widget( + SwipeToDeleteItem(text=f"One-line item {i}") + ) - TestCard().run() + Example().run() + + .. tab:: Decralative python styles + + .. code-block:: python + + from kivymd.app import MDApp + from kivymd.uix.boxlayout import MDBoxLayout + from kivymd.uix.button import MDIconButton + from kivymd.uix.card import ( + MDCardSwipe, MDCardSwipeLayerBox, MDCardSwipeFrontBox + ) + from kivymd.uix.list import MDList, OneLineListItem + from kivymd.uix.screen import MDScreen + from kivymd.uix.scrollview import MDScrollView + from kivymd.uix.toolbar import MDTopAppBar + + + class Example(MDApp): + def build(self): + self.theme_cls.theme_style = "Dark" + self.theme_cls.primary_palette = "Orange" + return ( + MDScreen( + MDBoxLayout( + MDTopAppBar( + elevation=4, + title="MDCardSwipe", + ), + MDScrollView( + MDList( + id="md_list", + ), + id="scroll", + scroll_timeout=100, + ), + id="box", + orientation="vertical", + ), + ) + ) + + def on_start(self): + '''Creates a list of cards.''' + + for i in range(20): + self.root.ids.box.ids.scroll.ids.md_list.add_widget( + MDCardSwipe( + MDCardSwipeLayerBox( + MDIconButton( + icon="trash-can", + pos_hint={"center_y": 0.5}, + on_release=self.remove_item, + ), + ), + MDCardSwipeFrontBox( + OneLineListItem( + id="content", + text=f"One-line item {i}", + _no_ripple_effect=True, + ) + ), + size_hint_y=None, + height="52dp", + ) + ) + + def remove_item(self, instance): + self.root.ids.box.ids.scroll.ids.md_list.remove_widget( + instance.parent.parent + ) + + + Example().run() .. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/handdelete-mdcard-swipe.gif :align: center @@ -457,6 +594,67 @@ Focus behavior MDCard: focus_behavior: True +.. tabs:: + + .. tab:: Declarative KV styles + + .. code-block:: python + + from kivy.lang import Builder + + from kivymd.app import MDApp + + KV = ''' + MDScreen: + + MDCard: + size_hint: .7, .4 + focus_behavior: True + pos_hint: {"center_x": .5, "center_y": .5} + md_bg_color: "darkgrey" + unfocus_color: "darkgrey" + focus_color: "grey" + elevation: 6 + ''' + + + class Example(MDApp): + def build(self): + self.theme_cls.theme_style = "Dark" + return Builder.load_string(KV) + + + Example().run() + + .. tab:: Declarative python styles + + .. code-block:: python + + from kivymd.app import MDApp + from kivymd.uix.card import MDCard + from kivymd.uix.screen import MDScreen + + + class Example(MDApp): + def build(self): + self.theme_cls.theme_style = "Dark" + return ( + MDScreen( + MDCard( + size_hint=(0.7, 0.4), + focus_behavior=True, + pos_hint={"center_x": 0.5, "center_y": 0.5}, + md_bg_color="darkgrey", + unfocus_color="darkgrey", + focus_color="grey", + elevation=6, + ), + ) + ) + + + Example().run() + .. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/card-focus.gif :align: center @@ -471,88 +669,6 @@ Ripple behavior .. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/card-behavior.gif :align: center -End full code -------------- - -.. code-block:: python - - from kivy.lang import Builder - - from kivymd.app import MDApp - - KV = ''' - - icon: "star" - on_release: self.icon = "star-outline" if self.icon == "star" else "star" - - - MDScreen: - - MDCard: - orientation: "vertical" - size_hint: .5, None - height: box_top.height + box_bottom.height - focus_behavior: True - ripple_behavior: True - pos_hint: {"center_x": .5, "center_y": .5} - - MDBoxLayout: - id: box_top - spacing: "20dp" - adaptive_height: True - - FitImage: - source: "/Users/macbookair/album.jpeg" - size_hint: .3, None - height: text_box.height - - MDBoxLayout: - id: text_box - orientation: "vertical" - adaptive_height: True - spacing: "10dp" - padding: 0, "10dp", "10dp", "10dp" - - MDLabel: - text: "Ride the Lightning" - theme_text_color: "Primary" - font_style: "H5" - bold: True - adaptive_height: True - - MDLabel: - text: "July 27, 1984" - adaptive_height: True - theme_text_color: "Primary" - - MDSeparator: - - MDBoxLayout: - id: box_bottom - adaptive_height: True - padding: "10dp", 0, 0, 0 - - MDLabel: - text: "Rate this album" - adaptive_height: True - pos_hint: {"center_y": .5} - theme_text_color: "Primary" - - StarButton: - StarButton: - StarButton: - StarButton: - StarButton: - ''' - - - class Test(MDApp): - def build(self): - self.theme_cls.theme_style = "Dark" - return Builder.load_string(KV) - - - Test().run() """ __all__ = ( @@ -579,17 +695,20 @@ from kivy.properties import ( VariableListProperty, ) from kivy.uix.boxlayout import BoxLayout -from kivy.uix.relativelayout import RelativeLayout +from kivy.utils import get_color_from_hex from kivymd import uix_path from kivymd.color_definitions import colors from kivymd.theming import ThemableBehavior from kivymd.uix.behaviors import ( BackgroundColorBehavior, - FocusBehavior, + CommonElevationBehavior, + DeclarativeBehavior, RectangularRippleBehavior, ) +from kivymd.uix.behaviors.focus_behavior import FocusBehavior from kivymd.uix.boxlayout import MDBoxLayout +from kivymd.uix.relativelayout import MDRelativeLayout with open( os.path.join(uix_path, "card", "card.kv"), encoding="utf-8" @@ -602,7 +721,7 @@ class MDSeparator(ThemableBehavior, MDBoxLayout): color = ColorProperty(None) """ - Separator color in ``rgba`` format. + Separator color. :attr:`color` is a :class:`~kivy.properties.ColorProperty` and defaults to `None`. @@ -623,9 +742,11 @@ class MDSeparator(ThemableBehavior, MDBoxLayout): class MDCard( + DeclarativeBehavior, ThemableBehavior, BackgroundColorBehavior, RectangularRippleBehavior, + CommonElevationBehavior, FocusBehavior, BoxLayout, ): @@ -645,14 +766,6 @@ class MDCard( and defaults to `False`. """ - elevation = NumericProperty(None, allownone=True) - """ - Elevation value. - - :attr:`elevation` is an :class:`~kivy.properties.NumericProperty` - and defaults to 1. - """ - radius = VariableListProperty([dp(6), dp(6), dp(6), dp(6)]) """ Card radius by default. @@ -676,15 +789,16 @@ class MDCard( """ _bg_color_map = ( - colors["Light"]["CardsDialogs"], - colors["Dark"]["CardsDialogs"], + get_color_from_hex(colors["Light"]["CardsDialogs"]), + get_color_from_hex(colors["Dark"]["CardsDialogs"]), [1.0, 1.0, 1.0, 0.0], ) - def __init__(self, **kwargs): - super().__init__(**kwargs) - self.theme_cls.bind(theme_style=self.update_md_bg_color) - self.theme_cls.bind(material_style=self.set_style) + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + self.theme_cls.bind( + material_style=self.set_style, theme_style=self.update_md_bg_color + ) Clock.schedule_once(self.set_style) Clock.schedule_once( lambda x: self.on_ripple_behavior(0, self.ripple_behavior) @@ -693,24 +807,26 @@ class MDCard( def update_md_bg_color(self, instance_card, theme_style: str) -> None: if self.md_bg_color in self._bg_color_map: - self.md_bg_color = colors[theme_style]["CardsDialogs"] + self.md_bg_color = get_color_from_hex( + colors[theme_style]["CardsDialogs"] + ) def set_style(self, *args) -> None: self.set_radius() self.set_elevation() self.set_line_color() - def set_line_color(self): + def set_line_color(self) -> None: if self.theme_cls.material_style == "M3": if self.style == "elevated" or self.style == "filled": self.line_color = [0, 0, 0, 0] - def set_elevation(self): + def set_elevation(self) -> None: if self.theme_cls.material_style == "M3": if self.style == "outlined" or self.style == "filled": self.elevation = 0 elif self.style == "elevated": - self.elevation = 1 + self.elevation = 2 def set_radius(self) -> None: if ( @@ -730,7 +846,7 @@ class MDCard( self._no_ripple_effect = False if value_behavior else True -class MDCardSwipe(RelativeLayout): +class MDCardSwipe(MDRelativeLayout): """ :Events: :attr:`on_swipe_complete` @@ -765,6 +881,16 @@ class MDCardSwipe(RelativeLayout): and defaults to `'out_sine'`. """ + closing_interval = NumericProperty(0) + """ + Interval for closing the front layer. + + .. versionadded:: 1.1.0 + + :attr:`closing_interval` is a :class:`~kivy.properties.NumericProperty` + and defaults to `0`. + """ + anchor = OptionProperty("left", options=("left", "right")) """ Anchoring screen edge for card. Available options are: `'left'`, `'right'`. @@ -830,10 +956,11 @@ class MDCardSwipe(RelativeLayout): _opens_process = False _to_closed = True + _distance = 0 - def __init__(self, **kw): + def __init__(self, *args, **kwargs): self.register_event_type("on_swipe_complete") - super().__init__(**kw) + super().__init__(*args, **kwargs) def add_widget(self, widget, index=0, canvas=None): if isinstance(widget, (MDCardSwipeFrontBox, MDCardSwipeLayerBox)): @@ -853,18 +980,24 @@ class MDCardSwipe(RelativeLayout): def on_open_progress( self, instance_swipe_to_delete_item, progress_value: float ) -> None: - if self.anchor == "left": - self.children[0].x = self.width * progress_value - else: - self.children[0].x = self.width * progress_value - self.width + def on_open_progress(*args): + if self.anchor == "left": + self.children[0].x = self.width * progress_value + else: + self.children[0].x = self.width * progress_value - self.width + + Clock.schedule_once(on_open_progress) def on_touch_move(self, touch): if self.collide_point(touch.x, touch.y): - expr = ( - touch.x < self.swipe_distance - if self.anchor == "left" - else touch.x > self.width - self.swipe_distance - ) + self._distance += touch.dx + expr = False + + if self.anchor == "left" and touch.dx >= 0: + expr = abs(self._distance) < self.swipe_distance + elif self.anchor == "right" and touch.dx < 0: + expr = abs(self._distance) > self.swipe_distance + if expr and not self._opens_process: self._opens_process = True self._to_closed = False @@ -875,6 +1008,7 @@ class MDCardSwipe(RelativeLayout): return super().on_touch_move(touch) def on_touch_up(self, touch): + self._distance = 0 if self.collide_point(touch.x, touch.y): if not self._to_closed: self._opens_process = False @@ -885,7 +1019,7 @@ class MDCardSwipe(RelativeLayout): if self.collide_point(touch.x, touch.y): if self.state == "opened": self._to_closed = True - self.close_card() + Clock.schedule_once(self.close_card, self.closing_interval) return super().on_touch_down(touch) def complete_swipe(self) -> None: @@ -895,7 +1029,7 @@ class MDCardSwipe(RelativeLayout): else self.open_progress >= self.max_swipe_x ) if expr: - self.close_card() + Clock.schedule_once(self.close_card, self.closing_interval) else: self.open_card() @@ -915,7 +1049,7 @@ class MDCardSwipe(RelativeLayout): anim.start(self.children[0]) self.state = "opened" - def close_card(self) -> None: + def close_card(self, *args) -> None: anim = Animation(x=0, t=self.closing_transition, d=self.opening_time) anim.bind(on_complete=self._reset_open_progress) anim.start(self.children[0]) diff --git a/sbapp/kivymd/uix/carousel.py b/sbapp/kivymd/uix/carousel.py index 600df2b..4b56dba 100644 --- a/sbapp/kivymd/uix/carousel.py +++ b/sbapp/kivymd/uix/carousel.py @@ -57,8 +57,10 @@ MDCarousel from kivy.animation import Animation from kivy.uix.carousel import Carousel +from kivymd.uix.behaviors import DeclarativeBehavior -class MDCarousel(Carousel): + +class MDCarousel(DeclarativeBehavior, Carousel): """ based on kivy's carousel. @@ -68,8 +70,8 @@ class MDCarousel(Carousel): _scrolling = False - def __init__(self, **kwargs): - super().__init__(**kwargs) + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) self.register_event_type("on_slide_progress") self.register_event_type("on_slide_complete") diff --git a/sbapp/kivymd/uix/chip/__init__.py b/sbapp/kivymd/uix/chip/__init__.py index 86420ec..ec6050b 100644 --- a/sbapp/kivymd/uix/chip/__init__.py +++ b/sbapp/kivymd/uix/chip/__init__.py @@ -1 +1 @@ -from .chip import MDChip, MDChooseChip # NOQA F401 +from .chip import MDChip # NOQA F401 diff --git a/sbapp/kivymd/uix/chip/chip.py b/sbapp/kivymd/uix/chip/chip.py index 54f0b77..03e1a98 100755 --- a/sbapp/kivymd/uix/chip/chip.py +++ b/sbapp/kivymd/uix/chip/chip.py @@ -132,7 +132,7 @@ Use with elevation icon_right: "close-circle-outline" line_color: app.theme_cls.disabled_hint_text_color md_bg_color: 1, 0, 0, .5 - elevation: 12 + elevation: 4 .. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/chip-with-elevation.png :align: center @@ -304,7 +304,6 @@ __all__ = ("MDChip",) import os -from kivy import Logger from kivy.animation import Animation from kivy.lang import Builder from kivy.metrics import dp @@ -314,14 +313,13 @@ from kivy.uix.behaviors import ButtonBehavior from kivymd import uix_path from kivymd.theming import ThemableBehavior from kivymd.uix.behaviors import ( - FakeRectangularElevationBehavior, + CommonElevationBehavior, RectangularRippleBehavior, + ScaleBehavior, TouchBehavior, ) from kivymd.uix.boxlayout import MDBoxLayout from kivymd.uix.label import MDIcon -from kivymd.uix.stacklayout import MDStackLayout -from kivymd.uix.templates import ScaleWidget with open( os.path.join(uix_path, "chip", "chip.kv"), encoding="utf-8" @@ -330,12 +328,12 @@ with open( class MDChip( + MDBoxLayout, ThemableBehavior, RectangularRippleBehavior, - FakeRectangularElevationBehavior, - TouchBehavior, ButtonBehavior, - MDBoxLayout, + CommonElevationBehavior, + TouchBehavior, ): text = StringProperty() """ @@ -345,17 +343,6 @@ class MDChip( and defaults to `''`. """ - icon = StringProperty("checkbox-blank-circle", deprecated=True) - """ - Chip icon. - - .. deprecated:: 1.0.0 - Use :attr:`icon_right` and :attr:`icon_left` instead. - - :attr:`icon` is an :class:`~kivy.properties.StringProperty` - and defaults to `'checkbox-blank-circle'`. - """ - icon_left = StringProperty() """ Chip left icon. @@ -376,16 +363,6 @@ class MDChip( and defaults to `''`. """ - color = ColorProperty(None, deprecated=True) - """ - Chip color in ``rgba`` format. - - .. deprecated:: 1.0.0 - - :attr:`color` is an :class:`~kivy.properties.ColorProperty` - and defaults to `None`. - """ - text_color = ColorProperty(None) """ Chip's text color in ``rgba`` format. @@ -394,17 +371,6 @@ class MDChip( and defaults to `None`. """ - icon_color = ColorProperty(None, deprecated=True) - """ - Chip's icon color in ``rgba`` format. - - .. deprecated:: 1.0.0 - Use :attr:`icon_right_color` and :attr:`icon_left_color` instead. - - :attr:`icon_color` is an :class:`~kivy.properties.ColorProperty` - and defaults to `None`. - """ - icon_right_color = ColorProperty(None) """ Chip's right icon color in ``rgba`` format. @@ -435,26 +401,6 @@ class MDChip( and defaults to `None`. """ - check = BooleanProperty(False, deprecated=True) - """ - If `True`, a checkmark is added to the left when touch to the chip. - - .. deprecated:: 1.0.0 - - :attr:`check` is an :class:`~kivy.properties.BooleanProperty` - and defaults to `False`. - """ - - selected_chip_color = ColorProperty(None, deprecated=True) - """ - The color of the chip that is currently selected in ``rgba`` format. - - .. deprecated:: 1.0.0 - - :attr:`selected_chip_color` is an :class:`~kivy.properties.ColorProperty` - and defaults to `None`. - """ - active = BooleanProperty(False) """ Whether the check is marked or not. @@ -508,16 +454,7 @@ class MDChip( self.active = False -class MDChooseChip(MDStackLayout): - def __init__(self, **kwargs): - super().__init__(**kwargs) - Logger.warning( - "MDChooseChip: " - "class is deprecated and will be removed in a future version" - ) - - -class MDScalableCheckIcon(MDIcon, ScaleWidget): +class MDScalableCheckIcon(MDIcon, ScaleBehavior): pos_hint = {"center_y": 0.5} diff --git a/sbapp/kivymd/uix/controllers/windowcontroller.py b/sbapp/kivymd/uix/controllers/windowcontroller.py index 9ace0c4..9a5dacd 100644 --- a/sbapp/kivymd/uix/controllers/windowcontroller.py +++ b/sbapp/kivymd/uix/controllers/windowcontroller.py @@ -2,6 +2,8 @@ Controllers/WindowController ============================ +.. versionadded:: 1.0.0 + Modules and classes that implement useful methods for getting information about the state of the current application window. @@ -33,16 +35,35 @@ Controlling the resizing direction of the application window from kivy.core.window import Window from kivy.core.window.window_sdl2 import WindowSDL +from kivy.metrics import dp class WindowController: def __init__(self): self.window_resizing_direction = "unknown" + self.real_device_type = "unknown" self.__width = Window.width Window.bind(on_resize=self._on_resize) + def on_size(self, instance, size: list) -> None: + """Called when the application screen size changes.""" + + window_width = size[0] + + if window_width < dp(500): + self.real_device_type = "mobile" + elif window_width < dp(1100): + self.real_device_type = "tablet" + else: + self.real_device_type = "desktop" + + def get_real_device_type(self) -> str: + """Returns the device type - 'mobile', 'tablet' or 'desktop'.""" + + return self.real_device_type + def get_window_width_resizing_direction(self) -> str: - """Return window width resizing direction - 'left' or 'right'""" + """Return window width resizing direction - 'left' or 'right'.""" return self.window_resizing_direction diff --git a/sbapp/kivymd/uix/datatables/datatables.kv b/sbapp/kivymd/uix/datatables/datatables.kv index 188ddde..a6a7220 100644 --- a/sbapp/kivymd/uix/datatables/datatables.kv +++ b/sbapp/kivymd/uix/datatables/datatables.kv @@ -1,5 +1,4 @@ #:import DEVICE_TYPE kivymd.material_resources.DEVICE_TYPE -#:import FakeRectangularElevationBehavior kivymd.uix.behaviors.FakeRectangularElevationBehavior @@ -66,7 +65,7 @@ size_hint_y: None height: self.minimum_height spacing: "4dp" - tooltip_text: root.text + tooltip_text: root.tooltip if root.tooltip else root.text BoxLayout: id: box @@ -175,7 +174,11 @@ font_size: "14sp" on_release: root.table_data.open_pagination_menu() text: - f"{root.table_data.rows_num if root.table_data.rows_num < len(root.table_data.row_data) else len(root.table_data.row_data)}" + "{}".format( \ + root.table_data.rows_num \ + if root.table_data.rows_num < len(root.table_data.row_data) else \ + len(root.table_data.row_data) \ + ) Widget: size_hint_x: None @@ -192,9 +195,11 @@ if root.theme_cls.theme_style == "Dark" else \ (0, 0, 0, 1) text: - f"1-" \ - f"{root.table_data.rows_num if root.table_data.rows_num > len(root.table_data.row_data) else len(root.table_data.row_data)} " \ - f"of {len(root.table_data.row_data)}" + "1-{} of {}".format( \ + root.table_data.rows_num \ + if root.table_data.rows_num > len(root.table_data.row_data) else \ + len(root.table_data.row_data), len(root.table_data.row_data) \ + ) MDIconButton: id: button_back @@ -217,7 +222,7 @@ on_release: root.table_data.set_next_row_data_parts("forward") - + diff --git a/sbapp/kivymd/uix/datatables/datatables.py b/sbapp/kivymd/uix/datatables/datatables.py index b574d06..1626283 100644 --- a/sbapp/kivymd/uix/datatables/datatables.py +++ b/sbapp/kivymd/uix/datatables/datatables.py @@ -11,19 +11,6 @@ Components/DataTables .. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/data-tables-previous.png :align: center -Warnings ---------- - -.. warning:: Data tables are still far from perfect. The class is in constant - change, because of optimizations and bug fixes. If you find a bug or have - an improvement you want to share, take some time and share your discoveries - with us over the main git repo. - Any help is well appreciated. - -.. warning:: In versions prior to `Kivy 2.1.0-dev0` exists an error in which is - the table has only one row in the current page, the table will only render - one column instead of the whole row. - .. note:: `MDDataTable` allows developers to sort the data provided by column. This happens thanks to the use of an external function that you can bind while you're defining the table columns. Be aware that the sorting function @@ -159,6 +146,15 @@ class CellHeader(MDTooltip, BoxLayout): and defaults to `''`. """ + tooltip = StringProperty() + """ + Tooltip containing descriptive text for the column. + If the tooltip is not provided, column `text` shall be used instead. + + :attr:`tooltip` is a :class:`~kivy.properties.StringProperty` + and defaults to `''`. + """ + # TODO: Added example. sort_action = ObjectProperty() """ @@ -340,11 +336,19 @@ class TableHeader(ThemableBehavior, ScrollView): CellHeader( text=col_heading[0], sort_action=col_heading[2], + tooltip=col_heading[3], width=self.cols_minimum[i], table_data=self.table_data, is_sorted=(col_heading[0] == self.sorted_on), sorted_order=self.sorted_order, ) + if len(col_heading) == 4 + else CellHeader( + text=col_heading[0], + sort_action=col_heading[2], + width=self.cols_minimum[i], + table_data=self.table_data, + ) if len(col_heading) == 3 else CellHeader( text=col_heading[0], @@ -356,6 +360,9 @@ class TableHeader(ThemableBehavior, ScrollView): else: # Sets the text in the first cell. self.ids.first_cell.text = col_heading[0] + self.ids.first_cell.tooltip = ( + col_heading[3] if len(col_heading) == 4 else "" + ) self.ids.first_cell.ids.separator.height = 0 self.ids.first_cell.width = self.cols_minimum[i] @@ -765,6 +772,9 @@ class TablePagination(ThemableBehavior, MDBoxLayout): class MDDataTable(ThemableBehavior, AnchorLayout): """ + See :class:`~kivy.uix.anchorlayout.AnchorLayout` class documentation for + more information. + :Events: :attr:`on_row_press` Called when a table row is clicked. @@ -775,7 +785,6 @@ class MDDataTable(ThemableBehavior, AnchorLayout): .. code-block:: python - from kivy.metrics import dp from kivymd.app import MDApp @@ -914,38 +923,82 @@ class MDDataTable(ThemableBehavior, AnchorLayout): """ Data for header columns. - .. code-block:: python + .. tabs:: - from kivy.metrics import dp + .. tab:: Imperative python style - from kivymd.app import MDApp - from kivymd.uix.datatables import MDDataTable - from kivy.uix.anchorlayout import AnchorLayout + .. code-block:: python + + from kivy.metrics import dp + + from kivymd.app import MDApp + from kivymd.uix.datatables import MDDataTable + from kivy.uix.anchorlayout import AnchorLayout - class Example(MDApp): - def build(self): - layout = AnchorLayout() - self.data_tables = MDDataTable( - size_hint=(0.7, 0.6), - use_pagination=True, - check=True, - # name column, width column, sorting function column(optional) - column_data=[ - ("No.", dp(30)), - ("Status", dp(30)), - ("Signal Name", dp(60)), - ("Severity", dp(30)), - ("Stage", dp(30)), - ("Schedule", dp(30), lambda *args: print("Sorted using Schedule")), - ("Team Lead", dp(30)), - ], - ) - layout.add_widget(self.data_tables) - return layout + class Example(MDApp): + def build(self): + self.theme_cls.theme_style = "Dark" + self.theme_cls.primary_palette = "Orange" + + layout = AnchorLayout() + self.data_tables = MDDataTable( + size_hint=(0.7, 0.6), + use_pagination=True, + check=True, + # name column, width column, sorting function column(optional), custom tooltip + column_data=[ + ("No.", dp(30), None, "Custom tooltip"), + ("Status", dp(30)), + ("Signal Name", dp(60)), + ("Severity", dp(30)), + ("Stage", dp(30)), + ("Schedule", dp(30), lambda *args: print("Sorted using Schedule")), + ("Team Lead", dp(30)), + ], + ) + layout.add_widget(self.data_tables) + return layout - Example().run() + Example().run() + + .. tab:: Declarative python style + + .. code-block:: python + + from kivy.metrics import dp + + from kivymd.app import MDApp + from kivymd.uix.anchorlayout import MDAnchorLayout + from kivymd.uix.datatables import MDDataTable + + + class Example(MDApp): + def build(self): + self.theme_cls.theme_style = "Dark" + self.theme_cls.primary_palette = "Orange" + return MDAnchorLayout( + MDDataTable( + size_hint=(0.7, 0.6), + use_pagination=True, + check=True, + # name column, width column, sorting function column(optional) + column_data=[ + ("No.", dp(30)), + ("Status", dp(30)), + ("Signal Name", dp(60)), + ("Severity", dp(30)), + ("Stage", dp(30)), + ("Schedule", dp(30), + lambda *args: print("Sorted using Schedule")), + ("Team Lead", dp(30)), + ], + ) + ) + + + Example().run() .. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/data-tables-column-data.png :align: center @@ -1060,6 +1113,9 @@ class MDDataTable(ThemableBehavior, AnchorLayout): class Example(MDApp): def build(self): + self.theme_cls.theme_style = "Dark" + self.theme_cls.primary_palette = "Orange" + layout = AnchorLayout() data_tables = MDDataTable( size_hint=(0.9, 0.6), @@ -1187,7 +1243,7 @@ class MDDataTable(ThemableBehavior, AnchorLayout): """ Use or not use checkboxes for rows. - .. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/data-tables-check.gif + .. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/data-tables-check.png :align: center :attr:`check` is an :class:`~kivy.properties.BooleanProperty` @@ -1209,6 +1265,9 @@ class MDDataTable(ThemableBehavior, AnchorLayout): class Example(MDApp): def build(self): + self.theme_cls.theme_style = "Dark" + self.theme_cls.primary_palette = "Orange" + layout = AnchorLayout() data_tables = MDDataTable( size_hint=(0.9, 0.6), @@ -1238,19 +1297,19 @@ class MDDataTable(ThemableBehavior, AnchorLayout): and defaults to `False`. """ - elevation = NumericProperty(8) + elevation = NumericProperty(4) """ Table elevation. :attr:`elevation` is an :class:`~kivy.properties.NumericProperty` - and defaults to `8`. + and defaults to `4`. """ rows_num = NumericProperty(5) """ The number of rows displayed on one page of the table. - .. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/data-tables-use-pagination.gif + .. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/data-tables-use-pagination-rows-num.png :align: center :attr:`rows_num` is an :class:`~kivy.properties.NumericProperty` @@ -1266,7 +1325,7 @@ class MDDataTable(ThemableBehavior, AnchorLayout): .. rubric:: Center - .. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/data-tables-menu-pos-center.png + .. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/data-tables-menu-pos-top.png :align: center .. rubric:: Auto @@ -1282,11 +1341,6 @@ class MDDataTable(ThemableBehavior, AnchorLayout): """ Menu height for selecting the number of displayed rows. - .. rubric:: 140dp - - .. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/data-tables-menu-height-140.png - :align: center - .. rubric:: 240dp .. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/data-tables-menu-height-240.png @@ -1298,7 +1352,7 @@ class MDDataTable(ThemableBehavior, AnchorLayout): background_color = ColorProperty([0, 0, 0, 0]) """ - Background color in the format (r, g, b, a). + Background color in the format (r, g, b, a) or string format. See :attr:`~kivy.uix.modalview.ModalView.background_color`. Use markup strings @@ -1315,6 +1369,9 @@ class MDDataTable(ThemableBehavior, AnchorLayout): class Example(MDApp): def build(self): + self.theme_cls.theme_style = "Dark" + self.theme_cls.primary_palette = "Orange" + layout = AnchorLayout() data_tables = MDDataTable( size_hint=(0.9, 0.6), @@ -1354,7 +1411,8 @@ class MDDataTable(ThemableBehavior, AnchorLayout): background_color_header = ColorProperty(None) """ - Background color for :class:`~TableHeader` class. + Background color in the format (r, g, b, a) or string format for + :class:`~TableHeader` class. .. versionadded:: 1.0.0 @@ -1374,7 +1432,8 @@ class MDDataTable(ThemableBehavior, AnchorLayout): background_color_cell = ColorProperty(None) """ - Background color for :class:`~CellRow` class. + Background color in the format (r, g, b, a) or string format for + :class:`~CellRow` class. .. versionadded:: 1.0.0 @@ -1395,7 +1454,8 @@ class MDDataTable(ThemableBehavior, AnchorLayout): background_color_selected_cell = ColorProperty(None) """ - Background selected color for :class:`~CellRow` class. + Background selected color in the format (r, g, b, a) or string format for + :class:`~CellRow` class. .. versionadded:: 1.0.0 @@ -1408,7 +1468,7 @@ class MDDataTable(ThemableBehavior, AnchorLayout): background_color_selected_cell="e4514f", ) - .. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/data-tables-background-color-selected-cell.gif + .. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/data-tables-background-color-selected-cell.png :align: center :attr:`background_color_selected_cell` is a :class:`~kivy.properties.ColorProperty` and @@ -1503,6 +1563,9 @@ class MDDataTable(ThemableBehavior, AnchorLayout): data_tables = None def build(self): + self.theme_cls.theme_style = "Dark" + self.theme_cls.primary_palette = "Orange" + layout = MDFloatLayout() # root layout # Creating control buttons. button_box = MDBoxLayout( @@ -1604,6 +1667,9 @@ class MDDataTable(ThemableBehavior, AnchorLayout): data_tables = None def build(self): + self.theme_cls.theme_style = "Dark" + self.theme_cls.primary_palette = "Orange" + layout = MDFloatLayout() layout.add_widget( MDRaisedButton( diff --git a/sbapp/kivymd/uix/dialog/dialog.kv b/sbapp/kivymd/uix/dialog/dialog.kv index 1f057a7..96e01a3 100644 --- a/sbapp/kivymd/uix/dialog/dialog.kv +++ b/sbapp/kivymd/uix/dialog/dialog.kv @@ -18,7 +18,11 @@ PopMatrix - + + shadow_color: 0.0, 0.0, 0.0, 0.0 + elevation: 0 + shadow_softness: 0 + shadow_offset: 0, 0 @@ -28,7 +32,6 @@ orientation: "vertical" size_hint_y: None height: self.minimum_height - elevation: 24 padding: "24dp", "24dp", "8dp", "8dp" radius: root.radius md_bg_color: diff --git a/sbapp/kivymd/uix/dialog/dialog.py b/sbapp/kivymd/uix/dialog/dialog.py index 10f4d67..f254f30 100755 --- a/sbapp/kivymd/uix/dialog/dialog.py +++ b/sbapp/kivymd/uix/dialog/dialog.py @@ -38,6 +38,8 @@ Usage dialog = None def build(self): + self.theme_cls.theme_style = "Dark" + self.theme_cls.primary_palette = "Orange" return Builder.load_string(KV) def show_alert_dialog(self): @@ -87,6 +89,7 @@ from kivy.uix.modalview import ModalView from kivymd import uix_path from kivymd.material_resources import DEVICE_TYPE from kivymd.theming import ThemableBehavior +from kivymd.uix.behaviors import CommonElevationBehavior from kivymd.uix.button import BaseButton from kivymd.uix.card import MDSeparator from kivymd.uix.list import BaseListItem @@ -97,7 +100,40 @@ with open( Builder.load_string(kv_file.read()) -class BaseDialog(ThemableBehavior, ModalView): +class BaseDialog(ThemableBehavior, ModalView, CommonElevationBehavior): + elevation = NumericProperty(3) + """ + See :attr:`kivymd.uix.behaviors.elevation.CommonElevationBehavior.elevation` + attribute for more information. + + .. versionadded:: 1.1.0 + + :attr:`elevation` is an :class:`~kivy.properties.NumericProperty` + and defaults to `3`. + """ + + shadow_softness = NumericProperty(24) + """ + See :attr:`kivymd.uix.behaviors.elevation.CommonElevationBehavior.shadow_softness` + attribute for more information. + + .. versionadded:: 1.1.0 + + :attr:`shadow_softness` is an :class:`~kivy.properties.NumericProperty` + and defaults to `24`. + """ + + shadow_offset = ListProperty((0, 4)) + """ + See :attr:`kivymd.uix.behaviors.elevation.CommonElevationBehavior.shadow_offset` + attribute for more information. + + .. versionadded:: 1.1.0 + + :attr:`shadow_offset` is an :class:`~kivy.properties.ListProperty` + and defaults to `[0, 4]`. + """ + radius = ListProperty([dp(7), dp(7), dp(7), dp(7)]) """ Dialog corners rounding value. @@ -250,21 +286,22 @@ class MDDialog(BaseDialog): class Example(MDApp): dialog = None - def build(self): - return Builder.load_string(KV) + def build(self): + self.theme_cls.theme_style = "Dark" + self.theme_cls.primary_palette = "Orange" + return Builder.load_string(KV) - def show_simple_dialog(self): - if not self.dialog: - self.dialog = MDDialog( - title="Set backup account", - type="simple", - items=[ - Item(text="user01@gmail.com", source="user-1.png"), - Item(text="user02@gmail.com", source="user-2.png"), - Item(text="Add account", source="add-icon.png"), - ], - ) - self.dialog.open() + def show_simple_dialog(self): + if not self.dialog: + self.dialog = MDDialog( + title="Set backup account", + type="simple", + items=[ + Item(text="user01@gmail.com", source="kivymd/images/logo/kivymd-icon-128.png"), + Item(text="user02@gmail.com", source="data/logo/kivy-icon-128.png"), + ], + ) + self.dialog.open() Example().run() @@ -317,6 +354,8 @@ class MDDialog(BaseDialog): dialog = None def build(self): + self.theme_cls.theme_style = "Dark" + self.theme_cls.primary_palette = "Orange" return Builder.load_string(KV) def show_confirmation_dialog(self): @@ -385,71 +424,140 @@ class MDDialog(BaseDialog): """ Custom content class. - .. code-block:: python + .. tabs:: - from kivy.lang import Builder - from kivy.uix.boxlayout import BoxLayout + .. tab:: Declarative KV style - from kivymd.app import MDApp - from kivymd.uix.button import MDFlatButton - from kivymd.uix.dialog import MDDialog + .. code-block:: python - KV = ''' - - orientation: "vertical" - spacing: "12dp" - size_hint_y: None - height: "120dp" + from kivy.lang import Builder + from kivy.uix.boxlayout import BoxLayout - MDTextField: - hint_text: "City" + from kivymd.app import MDApp + from kivymd.uix.button import MDFlatButton + from kivymd.uix.dialog import MDDialog - MDTextField: - hint_text: "Street" + KV = ''' + + orientation: "vertical" + spacing: "12dp" + size_hint_y: None + height: "120dp" + + MDTextField: + hint_text: "City" + + MDTextField: + hint_text: "Street" - MDFloatLayout: + MDFloatLayout: - MDFlatButton: - text: "ALERT DIALOG" - pos_hint: {'center_x': .5, 'center_y': .5} - on_release: app.show_confirmation_dialog() - ''' + MDFlatButton: + text: "ALERT DIALOG" + pos_hint: {'center_x': .5, 'center_y': .5} + on_release: app.show_confirmation_dialog() + ''' - class Content(BoxLayout): - pass + class Content(BoxLayout): + pass - class Example(MDApp): - dialog = None + class Example(MDApp): + dialog = None - def build(self): - return Builder.load_string(KV) + def build(self): + self.theme_cls.theme_style = "Dark" + self.theme_cls.primary_palette = "Orange" + return Builder.load_string(KV) - def show_confirmation_dialog(self): - if not self.dialog: - self.dialog = MDDialog( - title="Address:", - type="custom", - content_cls=Content(), - buttons=[ - MDFlatButton( - text="CANCEL", - theme_text_color="Custom", - text_color=self.theme_cls.primary_color, - ), - MDFlatButton( - text="OK", - theme_text_color="Custom", - text_color=self.theme_cls.primary_color, - ), - ], - ) - self.dialog.open() + def show_confirmation_dialog(self): + if not self.dialog: + self.dialog = MDDialog( + title="Address:", + type="custom", + content_cls=Content(), + buttons=[ + MDFlatButton( + text="CANCEL", + theme_text_color="Custom", + text_color=self.theme_cls.primary_color, + ), + MDFlatButton( + text="OK", + theme_text_color="Custom", + text_color=self.theme_cls.primary_color, + ), + ], + ) + self.dialog.open() - Example().run() + Example().run() + + .. tab:: Declarative Python style + + .. code-block:: python + + from kivymd.app import MDApp + from kivymd.uix.boxlayout import MDBoxLayout + from kivymd.uix.button import MDFlatButton + from kivymd.uix.dialog import MDDialog + from kivymd.uix.floatlayout import MDFloatLayout + from kivymd.uix.textfield import MDTextField + + + class Example(MDApp): + dialog = None + + def build(self): + self.theme_cls.theme_style = "Dark" + self.theme_cls.primary_palette = "Orange" + return ( + MDFloatLayout( + MDFlatButton( + text="ALERT DIALOG", + pos_hint={'center_x': 0.5, 'center_y': 0.5}, + on_release=self.show_confirmation_dialog, + ) + ) + ) + + def show_confirmation_dialog(self, *args): + if not self.dialog: + self.dialog = MDDialog( + title="Address:", + type="custom", + content_cls=MDBoxLayout( + MDTextField( + hint_text="City", + ), + MDTextField( + hint_text="Street", + ), + orientation="vertical", + spacing="12dp", + size_hint_y=None, + height="120dp", + ), + buttons=[ + MDFlatButton( + text="CANCEL", + theme_text_color="Custom", + text_color=self.theme_cls.primary_color, + ), + MDFlatButton( + text="OK", + theme_text_color="Custom", + text_color=self.theme_cls.primary_color, + ), + ], + ) + self.dialog.open() + + + Example().run() .. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/dialog-custom.png :align: center @@ -460,7 +568,7 @@ class MDDialog(BaseDialog): md_bg_color = ColorProperty(None) """ - Background color in the format (r, g, b, a). + Background color in the (r, g, b, a) or string format. :attr:`md_bg_color` is an :class:`~kivy.properties.ColorProperty` and defaults to `None`. diff --git a/sbapp/kivymd/uix/dropdownitem/dropdownitem.kv b/sbapp/kivymd/uix/dropdownitem/dropdownitem.kv index 4e6d163..82960e3 100644 --- a/sbapp/kivymd/uix/dropdownitem/dropdownitem.kv +++ b/sbapp/kivymd/uix/dropdownitem/dropdownitem.kv @@ -1,7 +1,7 @@ <_Triangle>: canvas: Color: - rgba: root.theme_cls.text_color + rgba: app.theme_cls.text_color Triangle: points: [ \ @@ -13,7 +13,8 @@ orientation: "vertical" - adaptive_size: True + size_hint: None, None + size: self.minimum_size spacing: "5dp" padding: "5dp", "5dp", "5dp", 0 diff --git a/sbapp/kivymd/uix/dropdownitem/dropdownitem.py b/sbapp/kivymd/uix/dropdownitem/dropdownitem.py index e8a98fb..3967c5c 100644 --- a/sbapp/kivymd/uix/dropdownitem/dropdownitem.py +++ b/sbapp/kivymd/uix/dropdownitem/dropdownitem.py @@ -15,13 +15,13 @@ Usage from kivymd.app import MDApp KV = ''' - Screen + MDScreen MDDropDownItem: id: drop_item pos_hint: {'center_x': .5, 'center_y': .5} text: 'Item' - on_release: self.set_item("New Item") + on_release: print("Press item") ''' @@ -48,12 +48,12 @@ import os from kivy.lang import Builder from kivy.properties import NumericProperty, StringProperty from kivy.uix.behaviors import ButtonBehavior +from kivy.uix.boxlayout import BoxLayout from kivy.uix.widget import Widget from kivymd import uix_path from kivymd.theming import ThemableBehavior -from kivymd.uix.behaviors import FakeRectangularElevationBehavior -from kivymd.uix.boxlayout import MDBoxLayout +from kivymd.uix.behaviors import DeclarativeBehavior with open( os.path.join(uix_path, "dropdownitem", "dropdownitem.kv"), encoding="utf-8" @@ -61,15 +61,12 @@ with open( Builder.load_string(kv_file.read()) -class _Triangle(ThemableBehavior, Widget): +class _Triangle(Widget): pass class MDDropDownItem( - ThemableBehavior, - FakeRectangularElevationBehavior, - ButtonBehavior, - MDBoxLayout, + DeclarativeBehavior, ThemableBehavior, ButtonBehavior, BoxLayout ): text = StringProperty() """ diff --git a/sbapp/kivymd/uix/expansionpanel/expansionpanel.py b/sbapp/kivymd/uix/expansionpanel/expansionpanel.py index 8a10a4a..2bbd8ee 100755 --- a/sbapp/kivymd/uix/expansionpanel/expansionpanel.py +++ b/sbapp/kivymd/uix/expansionpanel/expansionpanel.py @@ -56,6 +56,8 @@ Example .. code-block:: python + import os + from kivy.lang import Builder from kivymd.app import MDApp @@ -75,7 +77,7 @@ Example icon: 'phone' - ScrollView: + MDScrollView: MDGridLayout: id: box @@ -96,7 +98,7 @@ Example for i in range(10): self.root.ids.box.add_widget( MDExpansionPanel( - icon=f"{images_path}kivymd.png", + icon=os.path.join(images_path, "logo", "kivymd-icon-128.png"), content=Content(), panel_cls=MDExpansionPanelThreeLine( text="Text", diff --git a/sbapp/kivymd/uix/filemanager/filemanager.kv b/sbapp/kivymd/uix/filemanager/filemanager.kv index 57ab89b..ad5704f 100644 --- a/sbapp/kivymd/uix/filemanager/filemanager.kv +++ b/sbapp/kivymd/uix/filemanager/filemanager.kv @@ -6,28 +6,29 @@ background_normal: "" background_down: "" dir_or_file_name: "" + icon_color: 0, 0, 0, 0 _selected: False events_callback: lambda x: None orientation: "vertical" ModifiedOneLineIconListItem: text: root.dir_or_file_name + on_release: root.events_callback(root.path, root) bg_color: self.theme_cls.bg_darkest \ - if root._selected else self.theme_cls.bg_normal - on_release: root.events_callback(root.path, root) + if root._selected else \ + self.theme_cls.bg_normal IconLeftWidget: icon: root.icon - theme_text_color: "Custom" - text_color: self.theme_cls.primary_color + theme_icon_color: "Custom" + icon_color: root.icon_color MDSeparator: - size_hint_y: None - height: self.texture_size[1] + adaptive_height: True shorten: True shorten_from: "center" halign: "center" @@ -61,23 +62,6 @@ text: root.name - - anchor_x: "right" - anchor_y: "bottom" - size_hint_y: None - height: dp(56) - padding: dp(10) - - MDFloatingActionButton: - size_hint: None, None - size:dp(56), dp(56) - icon: root.icon - opposite_colors: True - elevation: 8 - on_release: root.callback() - md_bg_color: root.md_bg_color - - md_bg_color: root.theme_cls.bg_normal @@ -90,7 +74,11 @@ title: root.current_path right_action_items: [["close-box", lambda x: root.exit_manager(1)]] left_action_items: [["chevron-left", lambda x: root.back()]] - elevation: 10 + elevation: 3 + md_bg_color: + app.theme_cls.primary_color \ + if not root.background_color_toolbar else \ + root.background_color_toolbar RecycleView: id: rv diff --git a/sbapp/kivymd/uix/filemanager/filemanager.py b/sbapp/kivymd/uix/filemanager/filemanager.py index ab88234..1b7727e 100755 --- a/sbapp/kivymd/uix/filemanager/filemanager.py +++ b/sbapp/kivymd/uix/filemanager/filemanager.py @@ -9,7 +9,7 @@ Usage .. code-block:: python - path = '/' # path to the directory that will be opened in the file manager + path = os.path.expanduser("~") # path to the directory that will be opened in the file manager file_manager = MDFileManager( exit_manager=self.exit_manager, # function called when the user reaches directory tree root select_path=self.select_path, # function called when selecting a file/directory @@ -19,7 +19,7 @@ Usage .. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/file-manager.png :align: center -.. warning:: Be careful! To use the `/` path on Android devices, you need +.. warning:: Be careful! To use the `'/'` path on Android devices, you need special permissions. Therefore, you are likely to get an error. Or with ``preview`` mode: @@ -32,7 +32,7 @@ Or with ``preview`` mode: preview=True, ) -.. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/file-manager-previous.png +.. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/file-manager-preview.png :align: center .. warning:: The `preview` mode is intended only for viewing images and will @@ -43,6 +43,8 @@ Example .. code-block:: python + import os + from kivy.core.window import Window from kivy.lang import Builder @@ -53,19 +55,19 @@ Example KV = ''' MDBoxLayout: - orientation: 'vertical' + orientation: "vertical" MDTopAppBar: title: "MDFileManager" - left_action_items: [['menu', lambda x: None]] - elevation: 10 + left_action_items: [["menu", lambda x: None]] + elevation: 3 MDFloatLayout: MDRoundFlatIconButton: text: "Open manager" icon: "folder" - pos_hint: {'center_x': .5, 'center_y': .6} + pos_hint: {"center_x": .5, "center_y": .5} on_release: app.file_manager_open() ''' @@ -76,23 +78,23 @@ Example Window.bind(on_keyboard=self.events) self.manager_open = False self.file_manager = MDFileManager( - exit_manager=self.exit_manager, - select_path=self.select_path, - preview=True, + exit_manager=self.exit_manager, select_path=self.select_path ) def build(self): + self.theme_cls.theme_style = "Dark" + self.theme_cls.primary_palette = "Orange" return Builder.load_string(KV) def file_manager_open(self): - self.file_manager.show('/') # output manager to the screen + self.file_manager.show(os.path.expanduser("~")) # output manager to the screen self.manager_open = True - def select_path(self, path): - '''It will be called when you click on the file name + def select_path(self, path: str): + ''' + It will be called when you click on the file name or the catalog selection button. - :type path: str; :param path: path to the selected directory or file; ''' @@ -126,6 +128,9 @@ Not tested on `iOS`. def file_manager_open(self): self.file_manager.show_disks() + +.. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/file-manager-show-disks.png + :align: center """ __all__ = ("MDFileManager",) @@ -136,6 +141,7 @@ import re from typing import List, Tuple, Union from kivy import platform +from kivy.clock import Clock from kivy.factory import Factory from kivy.lang import Builder from kivy.metrics import dp @@ -148,7 +154,6 @@ from kivy.properties import ( OptionProperty, StringProperty, ) -from kivy.uix.anchorlayout import AnchorLayout from kivy.uix.behaviors import ButtonBehavior from kivy.uix.modalview import ModalView @@ -156,9 +161,10 @@ from kivymd import images_path, uix_path from kivymd.theming import ThemableBehavior from kivymd.uix.behaviors import CircularRippleBehavior from kivymd.uix.boxlayout import MDBoxLayout -from kivymd.uix.list import BaseListItem, ContainerSupport +from kivymd.uix.button import MDFloatingActionButton +from kivymd.uix.fitimage import FitImage +from kivymd.uix.list import BaseListItem from kivymd.uix.relativelayout import MDRelativeLayout -from kivymd.utils.fitimage import FitImage with open( os.path.join(uix_path, "filemanager", "filemanager.kv"), encoding="utf-8" @@ -167,9 +173,7 @@ with open( class BodyManager(MDBoxLayout): - """ - Base class for folders and files icons. - """ + """Base class for folders and files icons.""" class BodyManagerWithPreview(MDBoxLayout): @@ -182,40 +186,146 @@ class IconButton(CircularRippleBehavior, ButtonBehavior, FitImage): """Folder icons/thumbnails images in ``preview`` mode.""" -class FloatButton(AnchorLayout): - callback = ObjectProperty() - md_bg_color = ColorProperty([1, 1, 1, 1]) - icon = StringProperty() - - -class ModifiedOneLineIconListItem(ContainerSupport, BaseListItem): +class ModifiedOneLineIconListItem(BaseListItem): _txt_left_pad = NumericProperty("72dp") _txt_top_pad = NumericProperty("16dp") _txt_bot_pad = NumericProperty("15dp") _num_lines = 1 - def __init__(self, **kwargs): - super().__init__(**kwargs) + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) self.height = dp(48) -class MDFileManager(ThemableBehavior, MDRelativeLayout): - icon = StringProperty("check") +class MDFileManager(MDRelativeLayout, ThemableBehavior): """ - The icon that will be used on the directory selection button. + Implements a modal dialog with a file manager. + + For more information, see in the + :class:`~kivymd.uix.relativelayout.MDRelativeLayout` class documentation. + + :Events: + `on_pre_open`: + Called before the MDFileManager is opened. + `on_open`: + Called when the MDFileManager is opened. + `on_pre_dismiss`: + Called before the MDFileManager is closed. + `on_dismiss`: + Called when the MDFileManager is closed. + """ + + icon = StringProperty("check", deprecated=True) + """ + Icon that will be used on the directory selection button. + + .. deprecated:: 1.1.0 + Use :attr:`icon_selection_button` instead. :attr:`icon` is an :class:`~kivy.properties.StringProperty` and defaults to `check`. """ + icon_selection_button = StringProperty("check") + """ + Icon that will be used on the directory selection button. + + .. versionadded:: 1.1.0 + + .. code-block:: python + + MDFileManager( + ... + icon_selection_button="pencil", + ) + + .. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/file-manager-icon-selection-button.png + :align: center + + :attr:`icon_selection_button` is an :class:`~kivy.properties.StringProperty` + and defaults to `check`. + """ + + background_color_selection_button = ColorProperty(None) + """ + Background color of the current directory/path selection button. + + .. versionadded:: 1.1.0 + + .. code-block:: python + + MDFileManager( + ... + background_color_selection_button="brown", + ) + + .. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/file-manager-background-color-selection-button.png + :align: center + + :attr:`background_color_selection_button` is an :class:`~kivy.properties.ColorProperty` + and defaults to `None`. + """ + + background_color_toolbar = ColorProperty(None) + """ + Background color of the file manager toolbar. + + .. versionadded:: 1.1.0 + + .. code-block:: python + + MDFileManager( + ... + background_color_toolbar="brown", + ) + + .. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/file-manager-background-color-toolbar.png + :align: center + + :attr:`background_color_toolbar` is an :class:`~kivy.properties.ColorProperty` + and defaults to `None`. + """ + icon_folder = StringProperty(f"{images_path}folder.png") """ - The icon that will be used for folder icons when using ``preview = True``. + Icon that will be used for folder icons when using ``preview = True``. + + .. code-block:: python + + MDFileManager( + ... + preview=True, + icon_folder="path/to/icon.png", + ) + + .. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/file-manager-icon-folder.png + :align: center :attr:`icon` is an :class:`~kivy.properties.StringProperty` and defaults to `check`. """ + icon_color = ColorProperty(None) + """ + Color of the folder icon when the :attr:`preview` property is set to False. + + .. versionadded:: 1.1.0 + + .. code-block:: python + + MDFileManager( + ... + preview=False, + icon_color="brown", + ) + + .. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/file-manager-icon-color.png + :align: center + + :attr:`icon_color` is an :class:`~kivy.properties.ColorProperty` + and defaults to `None`. + """ + exit_manager = ObjectProperty(lambda x: None) """ Function called when the user reaches directory tree root. @@ -252,12 +362,12 @@ class MDFileManager(ThemableBehavior, MDRelativeLayout): and defaults to `all`. """ - current_path = StringProperty(os.getcwd()) + current_path = StringProperty(os.path.expanduser("~")) """ Current directory. :attr:`current_path` is an :class:`~kivy.properties.StringProperty` - and defaults to `/`. + and defaults to `os.path.expanduser("~")`. """ use_access = BooleanProperty(True) @@ -288,9 +398,9 @@ class MDFileManager(ThemableBehavior, MDRelativeLayout): "name", options=["nothing", "name", "date", "size", "type"] ) """ - It can take the values 'nothing' 'name' 'date' 'size' 'type' - sorts files by option - By default, sort by name. - Available options are: `'nothing'`, `'name'`, `'date'`, `'size'`, `'type'`. + It can take the values 'nothing' 'name' 'date' 'size' 'type' - sorts files + by option. By default, sort by name. Available options are: + `'nothing'`, `'name'`, `'date'`, `'size'`, `'type'`. :attr:`sort_by` is an :class:`~kivy.properties.OptionProperty` and defaults to `name`. @@ -318,29 +428,33 @@ class MDFileManager(ThemableBehavior, MDRelativeLayout): """ Contains the list of files that are currently selected. - :attr:`selection` is a read-only :class:`~kivy.properties.ListProperty` and - defaults to `[]`. + :attr:`selection` is a read-only :class:`~kivy.properties.ListProperty` + and defaults to `[]`. + """ + + selection_button = ObjectProperty() + """ + The instance of the directory/path selection button. + + .. versionadded:: 1.1.0 + + :attr:`selection_button` is a read-only :class:`~kivy.properties.ObjectProperty` + and defaults to `None`. """ _window_manager = None _window_manager_open = False - def __init__(self, **kwargs): - super().__init__(**kwargs) + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + self.register_event_type("on_pre_open") + self.register_event_type("on_open") + self.register_event_type("on_pre_dismiss") + self.register_event_type("on_dismiss") + toolbar_label = self.ids.toolbar.children[1].children[0] toolbar_label.font_style = "Subtitle1" - if ( - self.selector == "any" - or self.selector == "multi" - or self.selector == "folder" - ): - self.add_widget( - FloatButton( - callback=self.select_directory_on_press_button, - md_bg_color=self.theme_cls.primary_color, - icon=self.icon, - ) - ) + Clock.schedule_once(self._create_selection_button) if self.preview: self.ext = [".png", ".jpg", ".jpeg"] @@ -393,15 +507,7 @@ class MDFileManager(ThemableBehavior, MDRelativeLayout): } ) self.ids.rv.data = manager_list - - if not self._window_manager: - self._window_manager = ModalView( - size_hint=self.size_hint, auto_dismiss=False - ) - self._window_manager.add_widget(self) - if not self._window_manager_open: - self._window_manager.open() - self._window_manager_open = True + self._show() def show(self, path: str) -> None: """ @@ -467,6 +573,9 @@ class MDFileManager(ThemableBehavior, MDRelativeLayout): "icon": icon, "dir_or_file_name": name, "events_callback": self.select_dir_or_file, + "icon_color": self.theme_cls.primary_color + if not self.icon_color + else self.icon_color, "_selected": False, } ) @@ -481,19 +590,14 @@ class MDFileManager(ThemableBehavior, MDRelativeLayout): "icon": "file-outline", "dir_or_file_name": os.path.split(name)[1], "events_callback": self.select_dir_or_file, + "icon_color": self.theme_cls.primary_color + if not self.icon_color + else self.icon_color, "_selected": False, } ) self.ids.rv.data = manager_list - - if not self._window_manager: - self._window_manager = ModalView( - size_hint=self.size_hint, auto_dismiss=False - ) - self._window_manager.add_widget(self) - if not self._window_manager_open: - self._window_manager.open() - self._window_manager_open = True + self._show() def get_access_string(self, path: str) -> str: access_string = "" @@ -550,7 +654,9 @@ class MDFileManager(ThemableBehavior, MDRelativeLayout): def close(self) -> None: """Closes the file manager window.""" + self.dispatch("on_pre_dismiss") self._window_manager.dismiss() + self.dispatch("on_dismiss") self._window_manager_open = False def select_dir_or_file( @@ -602,6 +708,84 @@ class MDFileManager(ThemableBehavior, MDRelativeLayout): if self.selector == "folder" or self.selector == "any": self.select_path(self.current_path) + def on_icon(self, instance_file_manager, icon_name: str) -> None: + """Called when the :attr:`icon` property is changed.""" + + self.icon_selection_button = icon_name + + def on_background_color_toolbar( + self, instance_file_manager, color: Union[str, list] + ) -> None: + """ + Called when the :attr:`background_color_toolbar` property is changed. + """ + + def on_background_color_toolbar(*args): + self.ids.toolbar.md_bg_color = color + + Clock.schedule_once(on_background_color_toolbar) + + def on_pre_open(self, *args): + """ + Default pre-open event handler. + + .. versionadded:: 1.1.0 + """ + + def on_open(self, *args): + """ + Default open event handler. + + .. versionadded:: 1.1.0 + """ + + def on_pre_dismiss(self, *args): + """ + Default pre-dismiss event handler. + + .. versionadded:: 1.1.0 + """ + + def on_dismiss(self, *args): + """ + Default dismiss event handler. + + .. versionadded:: 1.1.0 + """ + + def _show(self): + if not self._window_manager: + self._window_manager = ModalView( + size_hint=self.size_hint, auto_dismiss=False + ) + self.size_hint = (1, 1) + self._window_manager.add_widget(self) + + if not self._window_manager_open: + self._window_manager.open() + self._window_manager_open = True + + self.dispatch("on_pre_open") + self.dispatch("on_open") + + def _create_selection_button(self, *args): + if ( + self.selector == "any" + or self.selector == "multi" + or self.selector == "folder" + ): + self.selection_button = MDFloatingActionButton( + on_release=self.select_directory_on_press_button, + md_bg_color=self.theme_cls.primary_color + if not self.background_color_selection_button + else self.background_color_selection_button, + icon=self.icon_selection_button, + pos_hint={"right": 0.99}, + y=dp(12), + elevation=0, + ) + self.add_widget(self.selection_button) + def __sort_files(self, files): def sort_by_name(files): files.sort(key=locale.strxfrm) diff --git a/sbapp/kivymd/uix/fitimage/fitimage.py b/sbapp/kivymd/uix/fitimage/fitimage.py index f3c94ee..35ded4d 100644 --- a/sbapp/kivymd/uix/fitimage/fitimage.py +++ b/sbapp/kivymd/uix/fitimage/fitimage.py @@ -1,6 +1,6 @@ """ Components/FitImage -================== +=================== Feature to automatically crop a `Kivy` image to fit your layout Write by Benedikt Zwölfer @@ -11,20 +11,42 @@ Referene - https://gist.github.com/benni12er/95a45eb168fc33a4fcd2d545af692dad Example: ======== -.. code-block:: kv +.. tabs:: - MDBoxLayout: - size_hint_y: None - height: "200dp" - orientation: 'vertical' + .. tab:: Declarative KV styles - FitImage: - size_hint_y: 3 - source: 'images/img1.jpg' + .. code-block:: kv - FitImage: - size_hint_y: 1 - source: 'images/img2.jpg' + MDBoxLayout: + size_hint_y: None + height: "200dp" + orientation: 'vertical' + + FitImage: + size_hint_y: 3 + source: 'images/img1.jpg' + + FitImage: + size_hint_y: 1 + source: 'images/img2.jpg' + + .. tab:: Declarative python styles + + .. code-block:: python + + MDBoxLayout( + FitImage( + size_hint_y=.3, + source='images/img1.jpg', + ), + FitImage( + size_hint_y=.7, + source='images/img2.jpg', + ), + size_hint_y=None, + height="200dp", + orientation='vertical', + ) Example with round corners: =========================== @@ -32,46 +54,73 @@ Example with round corners: .. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/fitimage-round-corners.png :align: center -.. code-block:: python +.. tabs:: - from kivy.uix.modalview import ModalView - from kivy.lang import Builder + .. tab:: Declarative KV styles - from kivymd import images_path - from kivymd.app import MDApp - from kivymd.uix.card import MDCard + .. code-block:: python - Builder.load_string( - ''' - : - elevation: 10 - radius: [36, ] + from kivy.lang import Builder - FitImage: - id: bg_image - source: "images/bg.png" - size_hint_y: .35 - pos_hint: {"top": 1} - radius: 36, 36, 0, 0 - ''') + from kivymd.app import MDApp + + KV = ''' + MDScreen: + + MDCard: + radius: 36 + md_bg_color: "grey" + pos_hint: {"center_x": .5, "center_y": .5} + size_hint: .4, .8 + + FitImage: + source: "bg.jpg" + size_hint_y: .35 + pos_hint: {"top": 1} + radius: 36, 36, 0, 0 + ''' - class Card(MDCard): - pass + class Example(MDApp): + def build(self): + self.theme_cls.theme_style = "Dark" + return Builder.load_string(KV) - class Example(MDApp): - def build(self): - modal = ModalView( - size_hint=(0.4, 0.8), - background=f"{images_path}/transparent.png", - overlay_color=(0, 0, 0, 0), - ) - modal.add_widget(Card()) - modal.open() + Example().run() + + .. tab:: Declarative python styles + + .. code-block:: python + + from kivymd.app import MDApp + from kivymd.uix.card import MDCard + from kivymd.uix.fitimage import FitImage + from kivymd.uix.screen import MDScreen - Example().run() + class Example(MDApp): + def build(self): + self.theme_cls.theme_style = "Dark" + return ( + MDScreen( + MDCard( + FitImage( + source="bg.jpg", + size_hint_y=0.35, + pos_hint={"top": 1}, + radius=(36, 36, 0, 0), + ), + radius=36, + md_bg_color="grey", + pos_hint={"center_x": .5, "center_y": .5}, + size_hint=(0.4, 0.8), + ), + ) + ) + + + Example().run() """ __all__ = ("FitImage",) @@ -80,14 +129,14 @@ from kivy.clock import Clock from kivy.graphics.context_instructions import Color from kivy.graphics.vertex_instructions import Rectangle from kivy.properties import BooleanProperty, ObjectProperty -from kivy.uix.boxlayout import BoxLayout from kivy.uix.image import AsyncImage from kivy.uix.widget import Widget -from kivymd.uix.templates import StencilWidget +from kivymd.uix.behaviors import StencilBehavior +from kivymd.uix.boxlayout import MDBoxLayout -class FitImage(BoxLayout, StencilWidget): +class FitImage(MDBoxLayout, StencilBehavior): source = ObjectProperty() """ Filename/source of your image. diff --git a/sbapp/kivymd/uix/floatlayout.py b/sbapp/kivymd/uix/floatlayout.py index df67386..3f99981 100644 --- a/sbapp/kivymd/uix/floatlayout.py +++ b/sbapp/kivymd/uix/floatlayout.py @@ -8,7 +8,7 @@ with some widget properties. For example: FloatLayout ----------- -.. code-block:: +.. code-block:: kv FloatLayout: canvas: @@ -22,7 +22,7 @@ FloatLayout MDFloatLayout ------------- -.. code-block:: +.. code-block:: kv MDFloatLayout: radius: [25, 0, 0, 0] @@ -36,7 +36,11 @@ MDFloatLayout from kivy.uix.floatlayout import FloatLayout from kivymd.uix import MDAdaptiveWidget +from kivymd.uix.behaviors import DeclarativeBehavior -class MDFloatLayout(FloatLayout, MDAdaptiveWidget): - pass +class MDFloatLayout(DeclarativeBehavior, FloatLayout, MDAdaptiveWidget): + """ + Float layout class. For more information, see in the + :class:`~kivy.uix.floatlayout.FloatLayout` class documentation. + """ diff --git a/sbapp/kivymd/uix/gridlayout.py b/sbapp/kivymd/uix/gridlayout.py index ea9df1e..84d883c 100644 --- a/sbapp/kivymd/uix/gridlayout.py +++ b/sbapp/kivymd/uix/gridlayout.py @@ -86,7 +86,11 @@ Equivalent from kivy.uix.gridlayout import GridLayout from kivymd.uix import MDAdaptiveWidget +from kivymd.uix.behaviors import DeclarativeBehavior -class MDGridLayout(GridLayout, MDAdaptiveWidget): - pass +class MDGridLayout(DeclarativeBehavior, GridLayout, MDAdaptiveWidget): + """ + Grid layout class. For more information, see in the + :class:`~kivy.uix.gridlayout.GridLayout` class documentation. + """ diff --git a/sbapp/kivymd/uix/hero.py b/sbapp/kivymd/uix/hero.py index 34eef37..70bb572 100644 --- a/sbapp/kivymd/uix/hero.py +++ b/sbapp/kivymd/uix/hero.py @@ -63,7 +63,7 @@ Base example x: 24 FitImage: - source: "https://github.com/kivymd/internal/raw/main/logo/kivymd_logo_blue.png" + source: "kivymd/images/logo/kivymd-icon-512.png" size_hint: None, None size: hero_from.size @@ -72,7 +72,7 @@ Base example pos_hint: {"center_x": .5} y: "36dp" on_release: - root.current_hero = "hero" + root.current_heroes = ["hero"] root.current = "screen B" MDScreen: @@ -82,6 +82,7 @@ Base example MDHeroTo: id: hero_to + tag: "hero" size_hint: None, None size: "220dp", "220dp" pos_hint: {"center_x": .5, "center_y": .5} @@ -91,7 +92,7 @@ Base example pos_hint: {"center_x": .5} y: "36dp" on_release: - root.current_hero = "hero" + root.current_heroes = ["hero"] root.current = "screen A" ''' @@ -113,6 +114,7 @@ Note that the child of the :class:`~MDHeroFrom` widget must have the size of the MDHeroFrom: id: hero_from + tag: "hero" FitImage: size_hint: None, None @@ -127,7 +129,7 @@ container in which the hero is located: MDRaisedButton: text: "Move Hero To Screen B" on_release: - root.current_hero = "hero" + root.current_heroes = ["hero"] root.current = "screen 2" If you need to switch to a screen that does not contain heroes, set the @@ -138,7 +140,7 @@ If you need to switch to a screen that does not contain heroes, set the MDRaisedButton: text: "Go To Another Screen" on_release: - root.current_hero = "" + root.current_heroes = [] root.current = "another screen" Example @@ -166,7 +168,7 @@ Example x: 24 FitImage: - source: "https://github.com/kivymd/internal/raw/main/logo/kivymd_logo_blue.png" + source: "kivymd/images/logo/kivymd-icon-512.png" size_hint: None, None size: hero_from.size @@ -175,7 +177,7 @@ Example pos_hint: {"center_x": .5} y: "36dp" on_release: - root.current_hero = "hero" + root.current_heroes = ["hero"] root.current = "screen B" MDScreen: @@ -185,6 +187,7 @@ Example MDHeroTo: id: hero_to + tag: "hero" size_hint: None, None size: "220dp", "220dp" pos_hint: {"center_x": .5, "center_y": .5} @@ -194,7 +197,7 @@ Example pos_hint: {"center_x": .5} y: "52dp" on_release: - root.current_hero = "" + root.current_heroes = [] root.current = "screen C" MDRaisedButton: @@ -202,7 +205,7 @@ Example pos_hint: {"center_x": .5} y: "8dp" on_release: - root.current_hero = "hero" + root.current_heroes = ["hero"] root.current = "screen A" MDScreen: @@ -283,7 +286,7 @@ background color of the hero during the flight between the screens: pos_hint: {"center_x": .5} y: "36dp" on_release: - root.current_hero = "hero" + root.current_heroes = ["hero"] root.current = "screen B" MDScreen: @@ -293,6 +296,7 @@ background color of the hero during the flight between the screens: MDHeroTo: id: hero_to + tag: "hero" size_hint: None, None size: "220dp", "220dp" pos_hint: {"center_x": .5, "center_y": .5} @@ -302,7 +306,7 @@ background color of the hero during the flight between the screens: pos_hint: {"center_x": .5} y: "36dp" on_release: - root.current_hero = "hero" + root.current_heroes = ["hero"] root.current = "screen A" ''' @@ -370,7 +374,7 @@ Usage with ScrollView radius: 24 box_radius: 0, 0, 24, 24 box_color: 0, 0, 0, .5 - source: "image.jpg" + source: "kivymd/images/logo/kivymd-icon-512.png" size_hint: None, None size: root.size mipmap: True @@ -399,7 +403,7 @@ Usage with ScrollView MDScreen: name: "screen B" - hero_to: hero_to + heroes_to: [hero_to] MDHeroTo: id: hero_to @@ -412,7 +416,7 @@ Usage with ScrollView pos_hint: {"center_x": .5} y: "36dp" on_release: - root.current_hero = "hero" + root.current_heroes = [hero_to.tag] root.current = "screen A" ''' @@ -441,7 +445,8 @@ Usage with ScrollView def on_release(self): def switch_screen(*args): - self.manager.current_hero = self.tag + self.manager.current_heroes = [self.tag] + self.manager.ids.hero_to.tag = self.tag self.manager.current = "screen B" Clock.schedule_once(switch_screen, 0.2) @@ -465,6 +470,93 @@ Usage with ScrollView .. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/hero-usage-with-scrollview.gif :align: center + +Using multiple heroes at the same time +-------------------------------------- + +.. code-block:: python + + from kivy.lang import Builder + + from kivymd.app import MDApp + + KV = ''' + MDScreenManager: + + MDScreen: + name: "screen A" + md_bg_color: "lightblue" + + MDHeroFrom: + id: hero_kivymd + tag: "kivymd" + size_hint: None, None + size: "200dp", "200dp" + pos_hint: {"top": .98} + x: 24 + + FitImage: + source: "kivymd/images/logo/kivymd-icon-512.png" + size_hint: None, None + size: hero_kivymd.size + + MDHeroFrom: + id: hero_kivy + tag: "kivy" + size_hint: None, None + size: "200dp", "200dp" + pos_hint: {"top": .98} + x: 324 + + FitImage: + source: "data/logo/kivy-icon-512.png" + size_hint: None, None + size: hero_kivy.size + + MDRaisedButton: + text: "Move Hero To Screen B" + pos_hint: {"center_x": .5} + y: "36dp" + on_release: + root.current_heroes = ["kivymd", "kivy"] + root.current = "screen B" + + MDScreen: + name: "screen B" + heroes_to: hero_to_kivymd, hero_to_kivy + md_bg_color: "cadetblue" + + MDHeroTo: + id: hero_to_kivy + tag: "kivy" + size_hint: None, None + pos_hint: {"center_x": .5, "center_y": .5} + + MDHeroTo: + id: hero_to_kivymd + tag: "kivymd" + size_hint: None, None + pos_hint: {"right": 1, "top": 1} + + MDRaisedButton: + text: "Move Hero To Screen A" + pos_hint: {"center_x": .5} + y: "36dp" + on_release: + root.current_heroes = ["kivy", "kivymd"] + root.current = "screen A" + ''' + + + class Test(MDApp): + def build(self): + return Builder.load_string(KV) + + + Test().run() + +.. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/hero-multiple-heroes.gif + :align: center """ from kivy.properties import StringProperty @@ -476,6 +568,9 @@ class MDHeroFrom(MDBoxLayout): """ The container from which the hero begins his flight. + For more information, see in the + :class:`~kivymd.uix.boxlayout.MDBoxLayout` class documentation. + :Events: `on_transform_in` when the hero flies from screen **A** to screen **B**. @@ -487,7 +582,7 @@ class MDHeroFrom(MDBoxLayout): """ Tag ID for heroes. - :attr:`shift_right` is an :class:`~kivy.properties.StringProperty` + :attr:`tag` is an :class:`~kivy.properties.StringProperty` and defaults to `''`. """ @@ -504,4 +599,17 @@ class MDHeroFrom(MDBoxLayout): class MDHeroTo(MDBoxLayout): - """The container in which the hero comes.""" + """ + The container in which the hero comes. + + For more information, see in the + :class:`~kivymd.uix.boxlayout.MDBoxLayout` class documentation. + """ + + tag = StringProperty(allownone=True) + """ + Tag ID for heroes. + + :attr:`tag` is an :class:`~kivy.properties.StringProperty` + and defaults to `''`. + """ diff --git a/sbapp/kivymd/uix/imagelist/__init__.py b/sbapp/kivymd/uix/imagelist/__init__.py index f8ba305..97c897c 100644 --- a/sbapp/kivymd/uix/imagelist/__init__.py +++ b/sbapp/kivymd/uix/imagelist/__init__.py @@ -1,2 +1,2 @@ # NOQA F401 -from .imagelist import MDSmartTile, SmartTileWithLabel, SmartTileWithStar +from .imagelist import MDSmartTile diff --git a/sbapp/kivymd/uix/imagelist/imagelist.py b/sbapp/kivymd/uix/imagelist/imagelist.py index b858404..970ed2e 100755 --- a/sbapp/kivymd/uix/imagelist/imagelist.py +++ b/sbapp/kivymd/uix/imagelist/imagelist.py @@ -65,12 +65,13 @@ Implementation :align: center """ -__all__ = ("MDSmartTile", "SmartTileWithLabel", "SmartTileWithStar") +__all__ = [ + "MDSmartTile", +] import os from kivy.lang import Builder -from kivy.logger import Logger from kivy.properties import ( BooleanProperty, ColorProperty, @@ -102,7 +103,7 @@ class SmartTileOverlayBox(MDBoxLayout): """Implements a container for custom widgets to be added to the tile.""" -class MDSmartTile(ThemableBehavior, MDRelativeLayout): +class MDSmartTile(MDRelativeLayout, ThemableBehavior): """ A tile for more complex needs. @@ -248,8 +249,8 @@ class MDSmartTile(ThemableBehavior, MDRelativeLayout): and defaults to `False`. """ - def __init__(self, **kwargs): - super().__init__(**kwargs) + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) self.register_event_type("on_release") self.register_event_type("on_press") @@ -270,33 +271,3 @@ class MDSmartTile(ThemableBehavior, MDRelativeLayout): widget.shorten = True widget.shorten_from = "right" self.ids.box.add_widget(widget) - - -class SmartTileWithLabel(MDSmartTile): - """ - .. deprecated:: 1.0.0 - Use :class:`~MDSmartTile` class instead. - """ - - def __init__(self, **kwargs): - super().__init__(**kwargs) - Logger.warning( - "KivyMD: " - "The `SmartTileWithLabel` class has been deprecated. " - "Use the `MDSmartTile` class instead`" - ) - - -class SmartTileWithStar(MDSmartTile): - """ - .. deprecated:: 1.0.0 - Use :class:`~MDSmartTile` class instead. - """ - - def __init__(self, **kwargs): - super().__init__(**kwargs) - Logger.warning( - "KivyMD: " - "The `SmartTileWithStar` class has been deprecated. " - "Use the `MDSmartTile` class instead`" - ) diff --git a/sbapp/kivymd/uix/label/label.kv b/sbapp/kivymd/uix/label/label.kv index b974336..71cac91 100644 --- a/sbapp/kivymd/uix/label/label.kv +++ b/sbapp/kivymd/uix/label/label.kv @@ -17,8 +17,14 @@ rgba: (1, 1, 1, 1) if self.source else (0, 0, 0, 0) Rectangle: source: self.source if self.source else None - pos: self.pos - size: self.size + pos: + self.pos \ + if not self.source else \ + (self.x - self._size[0] / 2, self.y) + size: + self._size \ + if self.source else \ + self.size font_style: "Icon" text: u"{}".format(md_icons[root.icon]) if root.icon in md_icons else "blank" diff --git a/sbapp/kivymd/uix/label/label.py b/sbapp/kivymd/uix/label/label.py index da164f7..fc34ba4 100755 --- a/sbapp/kivymd/uix/label/label.py +++ b/sbapp/kivymd/uix/label/label.py @@ -222,14 +222,18 @@ __all__ = ("MDLabel", "MDIcon") import os from typing import Union +from kivy.animation import Animation from kivy.clock import Clock +from kivy.graphics import Color, Rectangle from kivy.lang import Builder from kivy.metrics import sp from kivy.properties import ( AliasProperty, BooleanProperty, ColorProperty, + ListProperty, NumericProperty, + ObjectProperty, OptionProperty, StringProperty, ) @@ -239,6 +243,7 @@ from kivymd import uix_path from kivymd.theming import ThemableBehavior from kivymd.theming_dynamic_text import get_contrast_text_color from kivymd.uix import MDAdaptiveWidget +from kivymd.uix.behaviors import DeclarativeBehavior from kivymd.uix.floatlayout import MDFloatLayout __MDLabel_colors__ = { @@ -259,7 +264,7 @@ with open( Builder.load_string(kv_file.read()) -class MDLabel(ThemableBehavior, Label, MDAdaptiveWidget): +class MDLabel(DeclarativeBehavior, ThemableBehavior, Label, MDAdaptiveWidget): font_style = StringProperty("Body1") """ Label font style. @@ -321,6 +326,7 @@ class MDLabel(ThemableBehavior, Label, MDAdaptiveWidget): parent_background = ColorProperty(None) can_capitalize = BooleanProperty(True) + canvas_bg = ObjectProperty() def __init__(self, **kwargs): super().__init__(**kwargs) @@ -348,6 +354,7 @@ class MDLabel(ThemableBehavior, Label, MDAdaptiveWidget): font_info = self.theme_cls.font_styles[self.font_style] self.font_name = font_info[0] self.font_size = sp(font_info[1]) + if font_info[2] and self.can_capitalize: self._capitalizing = True else: @@ -373,29 +380,68 @@ class MDLabel(ThemableBehavior, Label, MDAdaptiveWidget): # generic None value it's not yet been set self._text_color_str = "" if theme_text_color == "Custom" and self.text_color: - self.color = self.text_color + color = self.text_color elif ( theme_text_color == "ContrastParentBackground" and self.parent_background ): - self.color = get_contrast_text_color(self.parent_background) + color = get_contrast_text_color(self.parent_background) else: - self.color = [0, 0, 0, 1] + color = [0, 0, 0, 1] - def on_text_color(self, instance_label, color: list) -> None: + if self.theme_cls.theme_style_switch_animation: + Animation( + color=color, + d=self.theme_cls.theme_style_switch_animation_duration, + t="linear", + ).start(self) + else: + self.color = color + + def on_text_color(self, instance_label, color: Union[list, str]) -> None: if self.theme_text_color == "Custom": - self.color = self.text_color + if self.theme_cls.theme_style_switch_animation: + Animation( + color=self.text_color, + d=self.theme_cls.theme_style_switch_animation_duration, + t="linear", + ).start(self) + else: + self.color = self.text_color def on_opposite_colors(self, *args) -> None: self.on_theme_text_color(self, self.theme_text_color) + def on_md_bg_color(self, instance_label, color: Union[list, str]) -> None: + self.canvas.remove_group("Background_instruction") + with self.canvas.before: + Color(rgba=color) + self.canvas_bg = Rectangle(pos=self.pos, size=self.size) + self.bind(pos=self.update_canvas_bg_pos) + + def on_size(self, instance_label, size: list) -> None: + if self.canvas_bg: + self.canvas_bg.size = size + + def update_canvas_bg_pos(self, instance_label, pos: list) -> None: + if self.canvas_bg: + self.canvas_bg.pos = pos + def _do_update_theme_color(self, *args): if self._text_color_str: - self.color = getattr(self.theme_cls, self._text_color_str) if not self.disabled: - self.color = getattr(self.theme_cls, self._text_color_str) + color = getattr(self.theme_cls, self._text_color_str) else: - self.color = getattr(self.theme_cls, "disabled_hint_text_color") + color = getattr(self.theme_cls, "disabled_hint_text_color") + + if self.theme_cls.theme_style_switch_animation: + Animation( + color=color, + d=self.theme_cls.theme_style_switch_animation_duration, + t="linear", + ).start(self) + else: + self.color = color class MDIcon(MDFloatLayout, MDLabel): @@ -455,11 +501,16 @@ class MDIcon(MDFloatLayout, MDLabel): and defaults to `None`. """ - def __init__(self, **kwargs): + _size = ListProperty((0, 0)) + + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + Clock.schedule_once(self.adjust_size) + + def adjust_size(self, *args) -> None: from kivymd.uix.selectioncontrol import MDCheckbox - super().__init__(**kwargs) if not isinstance(self, MDCheckbox): self.size_hint = (None, None) - self.size = self.texture_size + self._size = self.texture_size[1], self.texture_size[1] self.adaptive_size = True diff --git a/sbapp/kivymd/uix/list/__init__.py b/sbapp/kivymd/uix/list/__init__.py index b61f9d8..a0e061c 100644 --- a/sbapp/kivymd/uix/list/__init__.py +++ b/sbapp/kivymd/uix/list/__init__.py @@ -2,7 +2,6 @@ from .list import ( BaseListItem, CheckboxLeftWidget, - ContainerSupport, IconLeftWidget, IconLeftWidgetWithoutTouch, IconRightWidget, diff --git a/sbapp/kivymd/uix/list/list.py b/sbapp/kivymd/uix/list/list.py index 75591e4..9f02cab 100755 --- a/sbapp/kivymd/uix/list/list.py +++ b/sbapp/kivymd/uix/list/list.py @@ -57,6 +57,7 @@ based on the above classes. - OneLineAvatarListItem_ - TwoLineAvatarListItem_ - ThreeLineAvatarListItem_ + - OneLineIconListItem_ - TwoLineIconListItem_ - ThreeLineIconListItem_ @@ -68,35 +69,71 @@ based on the above classes. - TwoLineAvatarIconListItem_ - ThreeLineAvatarIconListItem_ +- OneLineRightIconListItem_ +- TwoLineRightIconListItem_ +- ThreeLineRightIconListItem_ + Usage ----- -.. code-block:: python +.. tabs:: - from kivy.lang import Builder + .. tab:: Declarative KV style - from kivymd.app import MDApp - from kivymd.uix.list import OneLineListItem + .. code-block:: python - KV = ''' - ScrollView: + from kivy.lang import Builder - MDList: - id: container - ''' + from kivymd.app import MDApp + from kivymd.uix.list import OneLineListItem + + KV = ''' + MDScrollView: + + MDList: + id: container + ''' - class Test(MDApp): - def build(self): - return Builder.load_string(KV) + class Example(MDApp): + def build(self): + self.theme_cls.theme_style = "Dark" + return Builder.load_string(KV) - def on_start(self): - for i in range(20): - self.root.ids.container.add_widget( - OneLineListItem(text=f"Single-line item {i}") - ) + def on_start(self): + for i in range(20): + self.root.ids.container.add_widget( + OneLineListItem(text=f"Single-line item {i}") + ) - Test().run() + Example().run() + + .. tab:: Declarative python style + + .. code-block:: python + + from kivymd.app import MDApp + from kivymd.uix.list import OneLineListItem + + + class Example(MDApp): + def build(self): + self.theme_cls.theme_style = "Dark" + return ( + MDScrollView( + MDList( + id="container" + ) + ) + ) + + def on_start(self): + for i in range(20): + self.root.ids.container.add_widget( + OneLineListItem(text=f"Single-line item {i}") + ) + + Example().run() .. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/lists.gif :align: center @@ -104,37 +141,76 @@ Usage Events of List -------------- -.. code-block:: python +.. tabs:: - from kivy.lang import Builder + .. tab:: Declarative KV style - from kivymd.app import MDApp + .. code-block:: python - KV = ''' - ScrollView: + from kivy.lang import Builder - MDList: + from kivymd.app import MDApp - OneLineAvatarIconListItem: - on_release: print("Click!") + KV = ''' + MDScrollView: - IconLeftWidget: - icon: "github" + MDList: - OneLineAvatarIconListItem: - on_release: print("Click 2!") + OneLineAvatarIconListItem: + on_release: print("Click!") - IconLeftWidget: - icon: "gitlab" - ''' + IconLeftWidget: + icon: "github" + + OneLineAvatarIconListItem: + on_release: print("Click 2!") + + IconLeftWidget: + icon: "gitlab" + ''' - class MainApp(MDApp): - def build(self): - return Builder.load_string(KV) + class Example(MDApp): + def build(self): + self.theme_cls.theme_style = "Dark" + return Builder.load_string(KV) - MainApp().run() + Example().run() + + .. tab:: Declarative python style + + .. code-block:: python + + from kivymd.app import MDApp + from kivymd.uix.scrollview import MDScrollView + from kivymd.uix.list import MDList, OneLineAvatarIconListItem, IconLeftWidget + + + class Example(MDApp): + def build(self): + self.theme_cls.theme_style = "Dark" + return ( + MDScrollView( + MDList( + OneLineAvatarIconListItem( + IconLeftWidget( + icon="github" + ), + on_release=lambda x: print("Click!") + ), + OneLineAvatarIconListItem( + IconLeftWidget( + icon="gitlab" + ), + on_release=lambda x: print("Click 2!") + ), + ) + ) + ) + + + Example().run() .. OneLineListItem: OneLineListItem @@ -179,62 +255,220 @@ ThreeLineListItem OneLineAvatarListItem --------------------- -.. code-block:: kv +.. tabs:: - OneLineAvatarListItem: - text: "Single-line item with avatar" + .. tab:: Declarative KV style - ImageLeftWidget: - source: "data/logo/kivy-icon-256.png" + .. code-block:: kv -.. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/lists-map.png + OneLineAvatarListItem: + text: "Single-line item with avatar" + + ImageLeftWidget: + source: "kivymd/images/logo/kivymd-icon-256.png" + + .. tab:: Declarative python style + + .. code-block:: python + + OneLineAvatarListItem( + ImageLeftWidget( + source="kivymd/images/logo/kivymd-icon-256.png" + ), + text="Single-line item with avatar", + ) + +.. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/OneLineAvatarListItem.png :align: center .. TwoLineAvatarListItem: TwoLineAvatarListItem --------------------- -.. code-block:: kv +.. tabs:: - TwoLineAvatarListItem: - text: "Two-line item with avatar" - secondary_text: "Secondary text here" + .. tab:: Declarative KV style - ImageLeftWidget: - source: "data/logo/kivy-icon-256.png" + .. code-block:: kv + + TwoLineAvatarListItem: + text: "Two-line item with avatar" + secondary_text: "Secondary text here" + + ImageLeftWidget: + source: "kivymd/images/logo/kivymd-icon-256.png" + + .. tab:: Declarative python style + + .. code-block:: python + + OneLineAvatarListItem( + ImageLeftWidget( + source="kivymd/images/logo/kivymd-icon-256.png" + ), + text="Single-line item with avatar", + secondary_text: "Secondary text here", + ) .. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/TwoLineAvatarListItem.png :align: center - .. ThreeLineAvatarListItem: ThreeLineAvatarListItem ----------------------- -.. code-block:: kv +.. tabs:: - ThreeLineAvatarListItem: - text: "Three-line item with avatar" - secondary_text: "Secondary text here" - tertiary_text: "fit more text than usual" + .. tab:: Declarative KV style - ImageLeftWidget: - source: "data/logo/kivy-icon-256.png" + .. code-block:: kv + + ThreeLineAvatarListItem: + text: "Three-line item with avatar" + secondary_text: "Secondary text here" + tertiary_text: "fit more text than usual" + + ImageLeftWidget: + source: "kivymd/images/logo/kivymd-icon-256.png" + + .. tab:: Declarative python style + + .. code-block:: python + + OneLineAvatarListItem( + ImageLeftWidget( + source="kivymd/images/logo/kivymd-icon-256.png" + ), + text="Single-line item with avatar", + secondary_text: "Secondary text here", + tertiary_text: "fit more text than usual" + ) .. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/ThreeLineAvatarListItem.png :align: center +.. OneLineRightIconListItem: +OneLineRightIconListItem +------------------------ + +.. tabs:: + + .. tab:: Declarative KV style + + .. code-block:: kv + + OneLineRightIconListItem: + text: "Single-line item with avatar" + + ImageRightWidget: + source: "kivymd/images/logo/kivymd-icon-256.png" + + .. tab:: Declarative python style + + .. code-block:: python + + OneLineRightIconListItem( + ImageRightWidget( + source="kivymd/images/logo/kivymd-icon-256.png" + ), + text="Single-line item with avatar", + ) + +.. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/OneLineRightIconListItem.png + :align: center + +.. TwoLineRightIconListItem: +TwoLineRightIconListItem +------------------------ + +.. tabs:: + + .. tab:: Declarative KV style + + .. code-block:: kv + + TwoLineRightIconListItem: + text: "Single-line item with avatar" + secondary_text: "Secondary text here" + + ImageRightWidget: + source: "kivymd/images/logo/kivymd-icon-256.png" + + .. tab:: Declarative python style + + .. code-block:: python + + TwoLineRightIconListItem( + ImageRightWidget( + source="kivymd/images/logo/kivymd-icon-256.png" + ), + text="Single-line item with avatar", + secondary_text: "Secondary text here", + ) + +.. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/TwoLineRightIconListItem.png + :align: center + +.. ThreeLineRightIconListItem: +ThreeLineRightIconListItem +-------------------------- + +.. tabs:: + + .. tab:: Declarative KV style + + .. code-block:: kv + + ThreeLineRightIconListItem: + text: "Single-line item with avatar" + secondary_text: "Secondary text here" + tertiary_text: "fit more text than usual" + + ImageRightWidget: + source: "kivymd/images/logo/kivymd-icon-256.png" + + .. tab:: Declarative python style + + .. code-block:: python + + ThreeLineRightIconListItem( + ImageRightWidget( + source="kivymd/images/logo/kivymd-icon-256.png" + ), + text="Single-line item with avatar", + secondary_text: "Secondary text here", + tertiary_text: "fit more text than usual", + ) + +.. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/ThreeLineRightIconListItem.png + :align: center + .. OneLineIconListItem: OneLineIconListItem ------------------- -.. code-block:: kv +.. tabs:: - OneLineIconListItem: - text: "Single-line item with avatar" + .. tab:: Declarative KV style - IconLeftWidget: - icon: "language-python" + .. code-block:: kv + + OneLineIconListItem: + text: "Single-line item with avatar" + + IconLeftWidget: + icon: "language-python" + + .. tab:: Declarative python style + + .. code-block:: python + + OneLineIconListItem( + IconLeftWidget( + icon="language-python" + ), + text="Single-line item with avatar" + ) .. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/OneLineIconListItem.png :align: center @@ -243,14 +477,30 @@ OneLineIconListItem TwoLineIconListItem ------------------- -.. code-block:: kv +.. tabs:: - TwoLineIconListItem: - text: "Two-line item with avatar" - secondary_text: "Secondary text here" + .. tab:: Declarative KV style - IconLeftWidget: - icon: "language-python" + .. code-block:: kv + + TwoLineIconListItem: + text: "Two-line item with avatar" + secondary_text: "Secondary text here" + + IconLeftWidget: + icon: "language-python" + + .. tab:: Declarative python style + + .. code-block:: python + + TwoLineIconListItem( + IconLeftWidget( + icon="language-python" + ), + text="Single-line item with avatar", + secondary_text: "Secondary text here" + ) .. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/TwoLineIconListItem.png :align: center @@ -259,15 +509,32 @@ TwoLineIconListItem ThreeLineIconListItem --------------------- -.. code-block:: kv +.. tabs:: - ThreeLineIconListItem: - text: "Three-line item with avatar" - secondary_text: "Secondary text here" - tertiary_text: "fit more text than usual" + .. tab:: Declarative KV style - IconLeftWidget: - icon: "language-python" + .. code-block:: kv + + ThreeLineIconListItem: + text: "Three-line item with avatar" + secondary_text: "Secondary text here" + tertiary_text: "fit more text than usual" + + IconLeftWidget: + icon: "language-python" + + .. tab:: Declarative python style + + .. code-block:: python + + ThreeLineIconListItem( + IconLeftWidget( + icon="language-python" + ), + text="Single-line item with avatar", + secondary_text: "Secondary text here", + tertiary_text: "fit more text than usual", + ) .. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/ThreeLineIconListItem.png :align: center @@ -276,16 +543,34 @@ ThreeLineIconListItem OneLineAvatarIconListItem ------------------------- -.. code-block:: kv +.. tabs:: - OneLineAvatarIconListItem: - text: "One-line item with avatar" + .. tab:: Declarative KV style - IconLeftWidget: - icon: "plus" + .. code-block:: kv - IconRightWidget: - icon: "minus" + OneLineAvatarIconListItem: + text: "One-line item with avatar" + + IconLeftWidget: + icon: "plus" + + IconRightWidget: + icon: "minus" + + .. tab:: Declarative python style + + .. code-block:: python + + OneLineAvatarIconListItem( + IconLeftWidget( + icon="plus" + ), + IconRightWidget( + icon="minus" + ), + text="Single-line item with avatar", + ) .. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/OneLineAvatarIconListItem.png :align: center @@ -294,17 +579,36 @@ OneLineAvatarIconListItem TwoLineAvatarIconListItem ------------------------- -.. code-block:: kv +.. tabs:: - TwoLineAvatarIconListItem: - text: "Two-line item with avatar" - secondary_text: "Secondary text here" + .. tab:: Declarative KV style - IconLeftWidget: - icon: "plus" + .. code-block:: kv - IconRightWidget: - icon: "minus" + TwoLineAvatarIconListItem: + text: "Two-line item with avatar" + secondary_text: "Secondary text here" + + IconLeftWidget: + icon: "plus" + + IconRightWidget: + icon: "minus" + + .. tab:: Declarative python style + + .. code-block:: python + + TwoLineAvatarIconListItem( + IconLeftWidget( + icon="plus" + ), + IconRightWidget( + icon="minus" + ), + text="Single-line item with avatar", + secondary_text: "Secondary text here", + ) .. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/TwoLineAvatarIconListItem.png :align: center @@ -313,18 +617,38 @@ TwoLineAvatarIconListItem ThreeLineAvatarIconListItem --------------------------- -.. code-block:: kv +.. tabs:: - ThreeLineAvatarIconListItem: - text: "Three-line item with avatar" - secondary_text: "Secondary text here" - tertiary_text: "fit more text than usual" + .. tab:: Declarative KV style - IconLeftWidget: - icon: "plus" + .. code-block:: kv - IconRightWidget: - icon: "minus" + ThreeLineAvatarIconListItem: + text: "Three-line item with avatar" + secondary_text: "Secondary text here" + tertiary_text: "fit more text than usual" + + IconLeftWidget: + icon: "plus" + + IconRightWidget: + icon: "minus" + + .. tab:: Declarative python style + + .. code-block:: python + + ThreeLineAvatarIconListItem( + IconLeftWidget( + icon="plus" + ), + IconRightWidget( + icon="minus" + ), + text="Single-line item with avatar", + secondary_text: "Secondary text here", + tertiary_text: "fit more text than usual", + ) .. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/ThreeLineAvatarIconListItem.png :align: center @@ -332,101 +656,196 @@ ThreeLineAvatarIconListItem Custom list item ---------------- -.. code-block:: python +.. tabs:: - from kivy.lang import Builder - from kivy.properties import StringProperty + .. tab:: Declarative KV style - from kivymd.app import MDApp - from kivymd.uix.list import IRightBodyTouch, OneLineAvatarIconListItem - from kivymd.uix.selectioncontrol import MDCheckbox - from kivymd.icon_definitions import md_icons + .. code-block:: python + + from kivy.lang import Builder + from kivy.properties import StringProperty + + from kivymd.app import MDApp + from kivymd.uix.list import IRightBodyTouch, OneLineAvatarIconListItem + from kivymd.uix.selectioncontrol import MDCheckbox + from kivymd.icon_definitions import md_icons - KV = ''' - : + KV = ''' + : - IconLeftWidget: - icon: root.icon + IconLeftWidget: + icon: root.icon - RightCheckbox: + RightCheckbox: - MDBoxLayout: + MDScrollView: - ScrollView: - - MDList: - id: scroll - ''' + MDList: + id: scroll + ''' - class ListItemWithCheckbox(OneLineAvatarIconListItem): - '''Custom list item.''' + class ListItemWithCheckbox(OneLineAvatarIconListItem): + '''Custom list item.''' - icon = StringProperty("android") + icon = StringProperty("android") - class RightCheckbox(IRightBodyTouch, MDCheckbox): - '''Custom right container.''' + class RightCheckbox(IRightBodyTouch, MDCheckbox): + '''Custom right container.''' - class MainApp(MDApp): - def build(self): - return Builder.load_string(KV) + class Example(MDApp): + def build(self): + self.theme_cls.theme_style = "Dark" + return Builder.load_string(KV) - def on_start(self): - icons = list(md_icons.keys()) - for i in range(30): - self.root.ids.scroll.add_widget( - ListItemWithCheckbox(text=f"Item {i}", icon=icons[i]) - ) + def on_start(self): + icons = list(md_icons.keys()) + for i in range(30): + self.root.ids.scroll.add_widget( + ListItemWithCheckbox(text=f"Item {i}", icon=icons[i]) + ) - MainApp().run() + Example().run() + + .. tab:: Declarative python style + + .. code-block:: python + + from kivymd.app import MDApp + from kivymd.uix.list import IRightBodyTouch, OneLineAvatarIconListItem + from kivymd.uix.selectioncontrol import MDCheckbox + from kivymd.uix.scrollview import MDScrollView + from kivymd.uix.list import MDList + from kivymd.icon_definitions import md_icons + + + class RightCheckbox(IRightBodyTouch, MDCheckbox): + '''Custom right container.''' + + + class Example(MDApp): + def build(self): + self.theme_cls.theme_style = "Dark" + return ( + MDScrollView( + MDList( + id="scroll" + ) + ) + ) + + def on_start(self): + icons = list(md_icons.keys()) + for i in range(30): + self.root.ids.scroll.add_widget( + OneLineAvatarIconListItem( + IconLeftWidget( + icon=icons[i] + ), + RightCheckbox(), + text=f"Item {i}", + ) + ) + + + Example().run() .. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/custom-list-item.png :align: center -.. code-block:: python +.. tabs:: - from kivy.lang import Builder + .. tab:: Declarative KV style - from kivymd.app import MDApp - from kivymd.uix.boxlayout import MDBoxLayout - from kivymd.uix.list import IRightBodyTouch + .. code-block:: python - KV = ''' - OneLineAvatarIconListItem: - text: "One-line item with avatar" - on_size: - self.ids._right_container.width = container.width - self.ids._right_container.x = container.width + from kivy.lang import Builder - IconLeftWidget: - icon: "cog" + from kivymd.app import MDApp + from kivymd.uix.boxlayout import MDBoxLayout + from kivymd.uix.list import IRightBodyTouch - YourContainer: - id: container + KV = ''' + OneLineAvatarIconListItem: + text: "One-line item with avatar" + on_size: + self.ids._right_container.width = container.width + self.ids._right_container.x = container.width - MDIconButton: - icon: "minus" + IconLeftWidget: + icon: "cog" - MDIconButton: - icon: "plus" - ''' + YourContainer: + id: container + + MDIconButton: + icon: "minus" + + MDIconButton: + icon: "plus" + ''' - class YourContainer(IRightBodyTouch, MDBoxLayout): - adaptive_width = True + class YourContainer(IRightBodyTouch, MDBoxLayout): + adaptive_width = True - class MainApp(MDApp): - def build(self): - return Builder.load_string(KV) + class Example(MDApp): + def build(self): + self.theme_cls.theme_style = "Dark" + return Builder.load_string(KV) - MainApp().run() + Example().run() + + .. tab:: Declarative python style + + .. code-block:: python + + from kivymd.app import MDApp + from kivymd.uix.boxlayout import MDBoxLayout + from kivymd.uix.list import IRightBodyTouch + from kivymd.uix.button import MDIconButton + from kivymd.uix.list import OneLineAvatarIconListItem, IconLeftWidget + + + class YourContainer(IRightBodyTouch, MDBoxLayout): + adaptive_width = True + + + class Example(MDApp): + def build(self): + self.theme_cls.theme_style = "Dark" + return ( + OneLineAvatarIconListItem( + IconLeftWidget( + icon="cog" + ), + YourContainer( + MDIconButton( + icon="minus" + ), + MDIconButton( + icon="plus" + ), + id="container" + ), + text="One-line item with avatar" + ) + ) + + def on_start(self): + container = self.root.ids.container + self.root.ids._right_container.width = container.width + container.x = container.width + + + Example().run() .. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/custom-list-right-container.png :align: center @@ -437,26 +856,56 @@ Behavior When using the `AvatarListItem` and `IconListItem` classes, when an icon is clicked, the event of this icon is triggered: -.. code-block:: kv +.. tabs:: - OneLineIconListItem: - text: "Single-line item with icon" + .. tab:: Declarative KV style - IconLeftWidget: - icon: "language-python" + .. code-block:: kv + + OneLineIconListItem: + text: "Single-line item with icon" + + IconLeftWidget: + icon: "language-python" + + .. tab:: Declarative python style + + .. code-block:: python + + OneLineIconListItem( + IconLeftWidget( + icon="language-python" + ), + text="Single-line item with avatar", + ) .. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/list-icon-trigger.gif :align: center You can disable the icon event using the `WithoutTouch` classes: -.. code-block:: kv +.. tabs:: - OneLineIconListItem: - text: "Single-line item with icon" + .. tab:: Declarative KV style - IconLeftWidgetWithoutTouch: - icon: "language-python" + .. code-block:: kv + + OneLineIconListItem: + text: "Single-line item with icon" + + IconLeftWidgetWithoutTouch: + icon: "language-python" + + .. tab:: Declarative python style + + .. code-block:: python + + OneLineIconListItem( + IconLeftWidgetWithoutTouch( + icon="language-python" + ), + text="Single-line item with avatar", + ) .. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/list-icon-without-trigger.gif :align: center @@ -504,21 +953,23 @@ from kivy.properties import ( NumericProperty, OptionProperty, StringProperty, + VariableListProperty, ) from kivy.uix.behaviors import ButtonBehavior +from kivy.uix.floatlayout import FloatLayout import kivymd.material_resources as m_res from kivymd import uix_path from kivymd.theming import ThemableBehavior from kivymd.uix.behaviors import ( CircularRippleBehavior, + DeclarativeBehavior, RectangularRippleBehavior, ) from kivymd.uix.button import MDIconButton -from kivymd.uix.floatlayout import MDFloatLayout +from kivymd.uix.fitimage import FitImage from kivymd.uix.gridlayout import MDGridLayout from kivymd.uix.selectioncontrol import MDCheckbox -from kivymd.utils.fitimage import FitImage with open( os.path.join(uix_path, "list", "list.kv"), encoding="utf-8" @@ -537,17 +988,17 @@ class MDList(MDGridLayout): _list_vertical_padding = NumericProperty("8dp") - def add_widget(self, widget, index=0, canvas=None): - super().add_widget(widget, index, canvas) - self.height += widget.height - - def remove_widget(self, widget): - super().remove_widget(widget) - self.height -= widget.height + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + self.adaptive_height = True class BaseListItem( - ThemableBehavior, RectangularRippleBehavior, ButtonBehavior, MDFloatLayout + DeclarativeBehavior, + ThemableBehavior, + RectangularRippleBehavior, + ButtonBehavior, + FloatLayout, ): """ Base class to all ListItems. Not supposed to be instantiated on its own. @@ -563,8 +1014,8 @@ class BaseListItem( text_color = ColorProperty(None) """ - Text color in ``rgba`` format used if :attr:`~theme_text_color` is set - to `'Custom'`. + Text color in (r, g, b, a) or string format used + if :attr:`~theme_text_color` is set to `'Custom'`. :attr:`text_color` is a :class:`~kivy.properties.ColorProperty` and defaults to `None`. @@ -572,7 +1023,9 @@ class BaseListItem( font_style = StringProperty("Subtitle1") """ - Text font style. See ``kivymd.font_definitions.py``. + Text font style. + See `font-definitions `_ + for more information. :attr:`font_style` is a :class:`~kivy.properties.StringProperty` and defaults to `'Subtitle1'`. @@ -580,7 +1033,7 @@ class BaseListItem( theme_text_color = StringProperty("Primary", allownone=True) """ - Theme text color in ``rgba`` format for primary text. + The name of the color scheme for for the primary text. :attr:`theme_text_color` is a :class:`~kivy.properties.StringProperty` and defaults to `'Primary'`. @@ -604,7 +1057,7 @@ class BaseListItem( secondary_text_color = ColorProperty(None) """ - Text color in ``rgba`` format used for secondary text + Text color in (r, g, b, a) or string format used for secondary text if :attr:`~secondary_theme_text_color` is set to `'Custom'`. :attr:`secondary_text_color` is a :class:`~kivy.properties.ColorProperty` @@ -613,7 +1066,7 @@ class BaseListItem( tertiary_text_color = ColorProperty(None) """ - Text color in ``rgba`` format used for tertiary text + Text color in (r, g, b, a) or string format used for tertiary text if :attr:`~tertiary_theme_text_color` is set to 'Custom'. :attr:`tertiary_text_color` is a :class:`~kivy.properties.ColorProperty` @@ -622,7 +1075,7 @@ class BaseListItem( secondary_theme_text_color = StringProperty("Secondary", allownone=True) """ - Theme text color for secondary text. + The name of the color scheme for for the secondary text. :attr:`secondary_theme_text_color` is a :class:`~kivy.properties.StringProperty` and defaults to `'Secondary'`. @@ -630,7 +1083,7 @@ class BaseListItem( tertiary_theme_text_color = StringProperty("Secondary", allownone=True) """ - Theme text color for tertiary text. + The name of the color scheme for for the tertiary text. :attr:`tertiary_theme_text_color` is a :class:`~kivy.properties.StringProperty` and defaults to `'Secondary'`. @@ -638,7 +1091,9 @@ class BaseListItem( secondary_font_style = StringProperty("Body1") """ - Font style for secondary line. See ``kivymd.font_definitions.py``. + Font style for secondary line. + See `font-definitions `_ + for more information. :attr:`secondary_font_style` is a :class:`~kivy.properties.StringProperty` and defaults to `'Body1'`. @@ -646,7 +1101,9 @@ class BaseListItem( tertiary_font_style = StringProperty("Body1") """ - Font style for tertiary line. See ``kivymd.font_definitions.py``. + Font style for tertiary line. + See `font-definitions `_ + for more information. :attr:`tertiary_font_style` is a :class:`~kivy.properties.StringProperty` and defaults to `'Body1'`. @@ -665,7 +1122,7 @@ class BaseListItem( divider_color = ColorProperty(None) """ - Divider color. + Divider color in (r, g, b, a) or string format. .. versionadded:: 1.0.0 @@ -675,18 +1132,81 @@ class BaseListItem( bg_color = ColorProperty(None) """ - Background color for menu item. + Background color for list item in (r, g, b, a) or string format. :attr:`bg_color` is a :class:`~kivy.properties.ColorProperty` and defaults to `None`. """ + radius = VariableListProperty([0], length=4) + """ + Canvas radius. + + .. code-block:: python + + # Top left corner slice. + MDBoxLayout: + md_bg_color: app.theme_cls.primary_color + radius: [25, 0, 0, 0] + + :attr:`radius` is an :class:`~kivy.properties.VariableListProperty` + and defaults to `[0, 0, 0, 0]`. + """ + _txt_left_pad = NumericProperty("16dp") _txt_top_pad = NumericProperty() _txt_bot_pad = NumericProperty() _txt_right_pad = NumericProperty(m_res.HORIZ_MARGINS) _num_lines = 3 _no_ripple_effect = BooleanProperty(False) + _touchable_widgets = ListProperty() + + def on_touch_down(self, touch): + if self.propagate_touch_to_touchable_widgets(touch, "down"): + return + super().on_touch_down(touch) + + def on_touch_move(self, touch, *args): + if self.propagate_touch_to_touchable_widgets(touch, "move", *args): + return + super().on_touch_move(touch, *args) + + def on_touch_up(self, touch): + if self.propagate_touch_to_touchable_widgets(touch, "up"): + return + super().on_touch_up(touch) + + def propagate_touch_to_touchable_widgets(self, touch, touch_event, *args): + triggered = False + for i in self._touchable_widgets: + if i.collide_point(touch.x, touch.y): + triggered = True + if touch_event == "down": + i.on_touch_down(touch) + elif touch_event == "move": + i.on_touch_move(touch, *args) + elif touch_event == "up": + i.on_touch_up(touch) + return triggered + + def add_widget(self, widget): + if issubclass(widget.__class__, ILeftBody): + self.ids._left_container.add_widget(widget) + elif issubclass(widget.__class__, ILeftBodyTouch): + self.ids._left_container.add_widget(widget) + self._touchable_widgets.append(widget) + elif issubclass(widget.__class__, IRightBody): + self.ids._right_container.add_widget(widget) + elif issubclass(widget.__class__, IRightBodyTouch): + self.ids._right_container.add_widget(widget) + self._touchable_widgets.append(widget) + else: + return super().add_widget(widget) + + def remove_widget(self, widget): + super().remove_widget(widget) + if widget in self._touchable_widgets: + self._touchable_widgets.remove(widget) class ILeftBody: @@ -721,72 +1241,16 @@ class IRightBodyTouch: """ -class ContainerSupport: - """ - Overrides ``add_widget`` in a ``ListItem`` to include support - for ``I*Body`` widgets when the appropiate containers are present. - """ - - _touchable_widgets = ListProperty() - - def add_widget(self, widget, index=0): - if issubclass(widget.__class__, ILeftBody): - self.ids._left_container.add_widget(widget) - elif issubclass(widget.__class__, ILeftBodyTouch): - self.ids._left_container.add_widget(widget) - self._touchable_widgets.append(widget) - elif issubclass(widget.__class__, IRightBody): - self.ids._right_container.add_widget(widget) - elif issubclass(widget.__class__, IRightBodyTouch): - self.ids._right_container.add_widget(widget) - self._touchable_widgets.append(widget) - else: - return super().add_widget(widget) - - def remove_widget(self, widget): - super().remove_widget(widget) - if widget in self._touchable_widgets: - self._touchable_widgets.remove(widget) - - def on_touch_down(self, touch): - if self.propagate_touch_to_touchable_widgets(touch, "down"): - return - super().on_touch_down(touch) - - def on_touch_move(self, touch, *args): - if self.propagate_touch_to_touchable_widgets(touch, "move", *args): - return - super().on_touch_move(touch, *args) - - def on_touch_up(self, touch): - if self.propagate_touch_to_touchable_widgets(touch, "up"): - return - super().on_touch_up(touch) - - def propagate_touch_to_touchable_widgets(self, touch, touch_event, *args): - triggered = False - for i in self._touchable_widgets: - if i.collide_point(touch.x, touch.y): - triggered = True - if touch_event == "down": - i.on_touch_down(touch) - elif touch_event == "move": - i.on_touch_move(touch, *args) - elif touch_event == "up": - i.on_touch_up(touch) - return triggered - - class OneLineListItem(BaseListItem): """A one line list item.""" _txt_top_pad = NumericProperty("16dp") - _txt_bot_pad = NumericProperty("15dp") # dp(20) - dp(5) + _txt_bot_pad = NumericProperty("15dp") _height = NumericProperty() _num_lines = 1 - def __init__(self, **kwargs): - super().__init__(**kwargs) + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) self.height = dp(48) if not self._height else self._height @@ -794,7 +1258,7 @@ class TwoLineListItem(BaseListItem): """A two line list item.""" _txt_top_pad = NumericProperty("20dp") - _txt_bot_pad = NumericProperty("15dp") # dp(20) - dp(5) + _txt_bot_pad = NumericProperty("15dp") _height = NumericProperty() def __init__(self, **kwargs): @@ -806,73 +1270,75 @@ class ThreeLineListItem(BaseListItem): """A three line list item.""" _txt_top_pad = NumericProperty("16dp") - _txt_bot_pad = NumericProperty("15dp") # dp(20) - dp(5) + _txt_bot_pad = NumericProperty("15dp") _height = NumericProperty() _num_lines = 3 - def __init__(self, **kwargs): - super().__init__(**kwargs) + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) self.height = dp(88) if not self._height else self._height -class OneLineAvatarListItem(ContainerSupport, BaseListItem): +class OneLineAvatarListItem(BaseListItem): _txt_left_pad = NumericProperty("72dp") _txt_top_pad = NumericProperty("20dp") - _txt_bot_pad = NumericProperty("19dp") # dp(24) - dp(5) + _txt_bot_pad = NumericProperty("19dp") _height = NumericProperty() _num_lines = 1 - def __init__(self, **kwargs): - super().__init__(**kwargs) + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) self.height = dp(56) if not self._height else self._height class TwoLineAvatarListItem(OneLineAvatarListItem): _txt_top_pad = NumericProperty("20dp") - _txt_bot_pad = NumericProperty("15dp") # dp(20) - dp(5) + _txt_bot_pad = NumericProperty("15dp") _height = NumericProperty() _num_lines = 2 - def __init__(self, **kwargs): - super().__init__(**kwargs) + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) self.height = dp(72) if not self._height else self._height -class ThreeLineAvatarListItem(ContainerSupport, ThreeLineListItem): +class ThreeLineAvatarListItem(ThreeLineListItem): _txt_left_pad = NumericProperty("72dp") + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) -class OneLineIconListItem(ContainerSupport, OneLineListItem): + +class OneLineIconListItem(OneLineListItem): _txt_left_pad = NumericProperty("72dp") class TwoLineIconListItem(OneLineIconListItem): _txt_top_pad = NumericProperty("20dp") - _txt_bot_pad = NumericProperty("15dp") # dp(20) - dp(5) + _txt_bot_pad = NumericProperty("15dp") _height = NumericProperty() _num_lines = 2 - def __init__(self, **kwargs): - super().__init__(**kwargs) + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) self.height = dp(72) if not self._height else self._height -class ThreeLineIconListItem(ContainerSupport, ThreeLineListItem): +class ThreeLineIconListItem(ThreeLineListItem): _txt_left_pad = NumericProperty("72dp") -class OneLineRightIconListItem(ContainerSupport, OneLineListItem): - # dp(40) = dp(16) + dp(24): +class OneLineRightIconListItem(OneLineListItem): _txt_right_pad = NumericProperty("40dp") - def __init__(self, **kwargs): - super().__init__(**kwargs) + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) self._txt_right_pad = dp(40) + m_res.HORIZ_MARGINS class TwoLineRightIconListItem(OneLineRightIconListItem): _txt_top_pad = NumericProperty("20dp") - _txt_bot_pad = NumericProperty("15dp") # dp(20) - dp(5) + _txt_bot_pad = NumericProperty("15dp") _height = NumericProperty() _num_lines = 2 @@ -881,8 +1347,7 @@ class TwoLineRightIconListItem(OneLineRightIconListItem): self.height = dp(72) if not self._height else self._height -class ThreeLineRightIconListItem(ContainerSupport, ThreeLineListItem): - # dp(40) = dp(16) + dp(24): +class ThreeLineRightIconListItem(ThreeLineListItem): _txt_right_pad = NumericProperty("40dp") def __init__(self, **kwargs): @@ -891,29 +1356,26 @@ class ThreeLineRightIconListItem(ContainerSupport, ThreeLineListItem): class OneLineAvatarIconListItem(OneLineAvatarListItem): - # dp(40) = dp(16) + dp(24): _txt_right_pad = NumericProperty("40dp") - def __init__(self, **kwargs): - super().__init__(**kwargs) + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) self._txt_right_pad = dp(40) + m_res.HORIZ_MARGINS class TwoLineAvatarIconListItem(TwoLineAvatarListItem): - # dp(40) = dp(16) + dp(24): _txt_right_pad = NumericProperty("40dp") - def __init__(self, **kwargs): - super().__init__(**kwargs) + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) self._txt_right_pad = dp(40) + m_res.HORIZ_MARGINS class ThreeLineAvatarIconListItem(ThreeLineAvatarListItem): - # dp(40) = dp(16) + dp(24): _txt_right_pad = NumericProperty("40dp") - def __init__(self, **kwargs): - super().__init__(**kwargs) + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) self._txt_right_pad = dp(40) + m_res.HORIZ_MARGINS diff --git a/sbapp/kivymd/uix/menu/menu.kv b/sbapp/kivymd/uix/menu/menu.kv index d4ac30e..f72f87d 100644 --- a/sbapp/kivymd/uix/menu/menu.kv +++ b/sbapp/kivymd/uix/menu/menu.kv @@ -1,4 +1,4 @@ -#:import STD_INC kivymd.material_resources.STANDARD_INCREMENT +#:import STANDARD_INCREMENT kivymd.material_resources.STANDARD_INCREMENT @@ -14,7 +14,7 @@ size_hint: None, None - width: root.width_mult * STD_INC + width: root.width_mult * STANDARD_INCREMENT bar_width: 0 key_viewclass: "viewclass" key_size: "height" @@ -28,7 +28,7 @@ orientation: "vertical" - + diff --git a/sbapp/kivymd/uix/menu/menu.py b/sbapp/kivymd/uix/menu/menu.py index ebe1461..102cf96 100755 --- a/sbapp/kivymd/uix/menu/menu.py +++ b/sbapp/kivymd/uix/menu/menu.py @@ -781,7 +781,7 @@ class MDDropdownMenu(ThemableBehavior, FloatLayout): and defaults to `'[dp(7)]'`. """ - elevation = NumericProperty(10) + elevation = NumericProperty(4) """ Elevation value of menu dialog. @@ -790,7 +790,7 @@ class MDDropdownMenu(ThemableBehavior, FloatLayout): .. code-block:: python self.menu = MDDropdownMenu( - elevation=16, + elevation=4, ..., ) @@ -798,7 +798,7 @@ class MDDropdownMenu(ThemableBehavior, FloatLayout): :align: center :attr:`elevation` is an :class:`~kivy.properties.NumericProperty` - and defaults to `10`. + and defaults to `4`. """ _start_coords = [] diff --git a/sbapp/kivymd/uix/navigationdrawer/navigationdrawer.py b/sbapp/kivymd/uix/navigationdrawer/navigationdrawer.py index 5dea20d..9ac5823 100755 --- a/sbapp/kivymd/uix/navigationdrawer/navigationdrawer.py +++ b/sbapp/kivymd/uix/navigationdrawer/navigationdrawer.py @@ -24,7 +24,7 @@ Anatomy MDNavigationLayout: - ScreenManager: + MDScreenManager: Screen_1: @@ -39,50 +39,108 @@ Anatomy A simple example ---------------- -.. code-block:: python +.. tabs:: - from kivy.lang import Builder + .. tab:: Declarative KV styles - from kivymd.uix.boxlayout import MDBoxLayout - from kivymd.app import MDApp + .. code-block:: python - KV = ''' - MDScreen: + from kivy.lang import Builder - MDNavigationLayout: + from kivymd.uix.boxlayout import MDBoxLayout + from kivymd.app import MDApp - ScreenManager: + KV = ''' + MDScreen: - MDScreen: + MDNavigationLayout: - MDTopAppBar: - title: "Navigation Drawer" - elevation: 10 - pos_hint: {"top": 1} - md_bg_color: "#e7e4c0" - specific_text_color: "#4a4939" - left_action_items: - [['menu', lambda x: nav_drawer.set_state("open")]] + MDScreenManager: + + MDScreen: + + MDTopAppBar: + title: "Navigation Drawer" + elevation: 4 + pos_hint: {"top": 1} + md_bg_color: "#e7e4c0" + specific_text_color: "#4a4939" + left_action_items: + [['menu', lambda x: nav_drawer.set_state("open")]] - MDNavigationDrawer: - id: nav_drawer - md_bg_color: "#f7f4e7" + MDNavigationDrawer: + id: nav_drawer + radius: (0, 16, 16, 0) - ContentNavigationDrawer: - ''' + ContentNavigationDrawer: + ''' - class ContentNavigationDrawer(MDBoxLayout): - pass + class ContentNavigationDrawer(MDBoxLayout): + pass - class TestNavigationDrawer(MDApp): - def build(self): - return Builder.load_string(KV) + class Example(MDApp): + def build(self): + self.theme_cls.theme_style = "Dark" + return Builder.load_string(KV) - TestNavigationDrawer().run() + Example().run() + + .. tab:: Declarative python styles + + .. code-block:: python + + from kivymd.app import MDApp + from kivymd.uix.boxlayout import MDBoxLayout + from kivymd.uix.navigationdrawer import MDNavigationLayout, MDNavigationDrawer + from kivymd.uix.screen import MDScreen + from kivymd.uix.screenmanager import MDScreenManager + from kivymd.uix.toolbar import MDTopAppBar + + + class ContentNavigationDrawer(MDBoxLayout): + pass + + + class Example(MDApp): + def build(self): + self.theme_cls.theme_style = "Dark" + return( + MDScreen( + MDNavigationLayout( + MDScreenManager( + MDScreen( + MDTopAppBar( + title="Navigation Drawer", + elevation=4, + pos_hint={"top": 1}, + md_bg_color="#e7e4c0", + specific_text_color="#4a4939", + left_action_items=[ + ['menu', lambda x: self.nav_drawer_open()] + ], + ) + + ) + ), + MDNavigationDrawer( + ContentNavigationDrawer(), + id="nav_drawer", + radius=(0, 16, 16, 0), + ), + ), + ), + ) + + def nav_drawer_open(self, *args): + nav_drawer = self.root.children[0].ids.nav_drawer + nav_drawer.set_state("open") + + + Example().run() .. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/navigation-drawer.gif :align: center @@ -90,211 +148,213 @@ A simple example .. Note:: :class:`~MDNavigationDrawer` is an empty :class:`~kivymd.uix.card.MDCard` panel. -Custom content for navigation drawer ------------------------------------- - -Let's extend the ``ContentNavigationDrawer`` class from the above example and -create content for our :class:`~MDNavigationDrawer` panel: - -.. code-block:: kv - - # Menu item in the DrawerList list. - - theme_text_color: "Custom" - on_release: self.parent.set_color_item(self) - - IconLeftWidget: - id: icon - icon: root.icon - theme_text_color: "Custom" - text_color: root.text_color - -.. code-block:: python - - class ItemDrawer(OneLineIconListItem): - icon = StringProperty() - -.. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/drawer-item.png - :align: center - -Top of ``ContentNavigationDrawer`` and ``DrawerList`` for menu items: - -.. code-block:: kv - - - orientation: "vertical" - padding: "8dp" - spacing: "8dp" - - AnchorLayout: - anchor_x: "left" - size_hint_y: None - height: avatar.height - - Image: - id: avatar - size_hint: None, None - size: "56dp", "56dp" - source: "kivymd.png" - - MDLabel: - text: "KivyMD library" - font_style: "Button" - size_hint_y: None - height: self.texture_size[1] - - MDLabel: - text: "kivydevelopment@gmail.com" - font_style: "Caption" - size_hint_y: None - height: self.texture_size[1] - - ScrollView: - - DrawerList: - id: md_list - -.. code-block:: python - - class ContentNavigationDrawer(BoxLayout): - pass - - - class DrawerList(ThemableBehavior, MDList): - def set_color_item(self, instance_item): - '''Called when tap on a menu item.''' - - # Set the color of the icon and text for the menu item. - for item in self.children: - if item.text_color == self.theme_cls.primary_color: - item.text_color = self.theme_cls.text_color - break - instance_item.text_color = self.theme_cls.primary_color - -.. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/drawer-top.png - :align: center - -Create a menu list for ``ContentNavigationDrawer``: - -.. code-block:: python - - def on_start(self): - icons_item = { - "folder": "My files", - "account-multiple": "Shared with me", - "star": "Starred", - "history": "Recent", - "checkbox-marked": "Shared with me", - "upload": "Upload", - } - for icon_name in icons_item.keys(): - self.root.ids.content_drawer.ids.md_list.add_widget( - ItemDrawer(icon=icon_name, text=icons_item[icon_name]) - ) - -.. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/drawer-work.gif - :align: center - Standard content for the navigation bar --------------------------------------- -.. code-block:: python +.. tabs:: - from kivy.lang import Builder + .. tab:: Declarative KV styles - from kivymd.app import MDApp + .. code-block:: python - KV = ''' - - focus_color: "#e7e4c0" - unfocus_color: "#f7f4e7" - text_color: "#4a4939" - icon_color: "#4a4939" - ripple_color: "#c5bdd2" - selected_color: "#0c6c4d" + from kivy.lang import Builder + + from kivymd.app import MDApp + + KV = ''' + + focus_color: "#e7e4c0" + text_color: "#4a4939" + icon_color: "#4a4939" + ripple_color: "#c5bdd2" + selected_color: "#0c6c4d" - - bg_color: "#f7f4e7" - text_color: "#4a4939" - icon_color: "#4a4939" - _no_ripple_effect: True + + text_color: "#4a4939" + icon_color: "#4a4939" + focus_behavior: False + selected_color: "#4a4939" + _no_ripple_effect: True - MDScreen: + MDScreen: - MDNavigationLayout: + MDNavigationLayout: - ScreenManager: + MDScreenManager: - MDScreen: + MDScreen: - MDTopAppBar: - title: "Navigation Drawer" - elevation: 10 - pos_hint: {"top": 1} - md_bg_color: "#e7e4c0" - specific_text_color: "#4a4939" - left_action_items: - [ \ - [ \ - 'menu', lambda x: \ - nav_drawer.set_state("open") \ - if nav_drawer.state == "close" else \ - nav_drawer.set_state("close") \ - ] \ - ] + MDTopAppBar: + title: "Navigation Drawer" + elevation: 4 + pos_hint: {"top": 1} + md_bg_color: "#e7e4c0" + specific_text_color: "#4a4939" + left_action_items: [["menu", lambda x: nav_drawer.set_state("open")]] - MDNavigationDrawer: - id: nav_drawer - radius: (0, 16, 16, 0) if self.anchor == "left" else (16, 0, 0, 16) - md_bg_color: "#f7f4e7" + MDNavigationDrawer: + id: nav_drawer + radius: (0, 16, 16, 0) - MDNavigationDrawerMenu: + MDNavigationDrawerMenu: - MDNavigationDrawerHeader: - title: "Header title" - title_color: "#4a4939" - text: "Header text" - title_color: "#4a4939" - spacing: "4dp" - padding: "12dp", 0, 0, "56dp" + MDNavigationDrawerHeader: + title: "Header title" + title_color: "#4a4939" + text: "Header text" + spacing: "4dp" + padding: "12dp", 0, 0, "56dp" - MDNavigationDrawerLabel: - text: "Mail" + MDNavigationDrawerLabel: + text: "Mail" - DrawerClickableItem: - icon: "gmail" - right_text: "+99" - text_right_color: "#4a4939" - text: "Inbox" + DrawerClickableItem: + icon: "gmail" + right_text: "+99" + text_right_color: "#4a4939" + text: "Inbox" - DrawerClickableItem: - icon: "send" - text: "Outbox" + DrawerClickableItem: + icon: "send" + text: "Outbox" - MDNavigationDrawerDivider: + MDNavigationDrawerDivider: - MDNavigationDrawerLabel: - text: "Labels" + MDNavigationDrawerLabel: + text: "Labels" - DrawerLabelItem: - icon: "information-outline" - text: "Label" + DrawerLabelItem: + icon: "information-outline" + text: "Label" - DrawerLabelItem: - icon: "information-outline" - text: "Label" - ''' + DrawerLabelItem: + icon: "information-outline" + text: "Label" + ''' - class TestNavigationDrawer(MDApp): - def build(self): - self.theme_cls.primary_palette = "Indigo" - return Builder.load_string(KV) + class Example(MDApp): + def build(self): + self.theme_cls.theme_style = "Dark" + return Builder.load_string(KV) - TestNavigationDrawer().run() + Example().run() + + .. tab:: Declarative python styles + + .. code-block:: python + + from kivymd.app import MDApp + from kivymd.uix.navigationdrawer import ( + MDNavigationLayout, + MDNavigationDrawer, + MDNavigationDrawerMenu, + MDNavigationDrawerHeader, + MDNavigationDrawerLabel, + MDNavigationDrawerDivider, + MDNavigationDrawerItem, + ) + from kivymd.uix.screen import MDScreen + from kivymd.uix.screenmanager import MDScreenManager + from kivymd.uix.toolbar import MDTopAppBar + + + class BaseNavigationDrawerItem(MDNavigationDrawerItem): + def __init__(self, **kwargs): + super().__init__(**kwargs) + self.radius = 24 + self.text_color = "#4a4939" + self.icon_color = "#4a4939" + self.focus_color = "#e7e4c0" + + + class DrawerLabelItem(BaseNavigationDrawerItem): + def __init__(self, **kwargs): + super().__init__(**kwargs) + self.focus_behavior = False + self._no_ripple_effect = True + self.selected_color = "#4a4939" + + + class DrawerClickableItem(BaseNavigationDrawerItem): + def __init__(self, **kwargs): + super().__init__(**kwargs) + self.ripple_color = "#c5bdd2" + self.selected_color = "#0c6c4d" + + + class Example(MDApp): + def build(self): + self.theme_cls.theme_style = "Dark" + return( + MDScreen( + MDNavigationLayout( + MDScreenManager( + MDScreen( + MDTopAppBar( + title="Navigation Drawer", + elevation=4, + pos_hint={"top": 1}, + md_bg_color="#e7e4c0", + specific_text_color="#4a4939", + left_action_items=[ + ['menu', lambda x: self.nav_drawer_open()] + ], + ) + + ) + ), + MDNavigationDrawer( + MDNavigationDrawerMenu( + MDNavigationDrawerHeader( + title="Header title", + title_color="#4a4939", + text="Header text", + spacing="4dp", + padding=("12dp", 0, 0, "56dp"), + ), + MDNavigationDrawerLabel( + text="Mail", + ), + DrawerClickableItem( + icon="gmail", + right_text="+99", + text_right_color="#4a4939", + text="Inbox", + ), + DrawerClickableItem( + icon="send", + text="Outbox", + ), + MDNavigationDrawerDivider(), + MDNavigationDrawerLabel( + text="Labels", + ), + DrawerLabelItem( + icon="information-outline", + text="Label", + ), + DrawerLabelItem( + icon="information-outline", + text="Label", + ), + ), + id="nav_drawer", + radius=(0, 16, 16, 0), + ) + ) + ) + ) + + def nav_drawer_open(self, *args): + nav_drawer = self.root.children[0].ids.nav_drawer + nav_drawer.set_state("open") + + + Example().run() .. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/navigation-drawer-standatd-content.gif :align: center @@ -302,83 +362,164 @@ Standard content for the navigation bar Switching screens in the ``ScreenManager`` and using the common ``MDTopAppBar`` ----------------------------------------------------------------------------- -.. code-block:: python +.. tabs:: - from kivy.lang import Builder - from kivy.properties import ObjectProperty + .. tab:: Declarative KV styles - from kivymd.app import MDApp - from kivymd.uix.boxlayout import MDBoxLayout + .. code-block:: python - KV = ''' - + from kivy.lang import Builder + from kivy.properties import ObjectProperty - ScrollView: + from kivymd.app import MDApp + from kivymd.uix.scrollview import MDScrollView - MDList: + KV = ''' + - OneLineListItem: - text: "Screen 1" - on_press: - root.nav_drawer.set_state("close") - root.screen_manager.current = "scr 1" + MDList: - OneLineListItem: - text: "Screen 2" - on_press: - root.nav_drawer.set_state("close") - root.screen_manager.current = "scr 2" - - - MDScreen: - - MDTopAppBar: - id: toolbar - pos_hint: {"top": 1} - elevation: 10 - title: "MDNavigationDrawer" - left_action_items: [["menu", lambda x: nav_drawer.set_state("open")]] - - MDNavigationLayout: - x: toolbar.height - - ScreenManager: - id: screen_manager - - MDScreen: - name: "scr 1" - - MDLabel: + OneLineListItem: text: "Screen 1" - halign: "center" + on_press: + root.nav_drawer.set_state("close") + root.screen_manager.current = "scr 1" - MDScreen: - name: "scr 2" - - MDLabel: + OneLineListItem: text: "Screen 2" - halign: "center" - - MDNavigationDrawer: - id: nav_drawer - - ContentNavigationDrawer: - screen_manager: screen_manager - nav_drawer: nav_drawer - ''' + on_press: + root.nav_drawer.set_state("close") + root.screen_manager.current = "scr 2" - class ContentNavigationDrawer(MDBoxLayout): - screen_manager = ObjectProperty() - nav_drawer = ObjectProperty() + MDScreen: + + MDTopAppBar: + pos_hint: {"top": 1} + elevation: 4 + title: "MDNavigationDrawer" + left_action_items: [["menu", lambda x: nav_drawer.set_state("open")]] + + MDNavigationLayout: + + MDScreenManager: + id: screen_manager + + MDScreen: + name: "scr 1" + + MDLabel: + text: "Screen 1" + halign: "center" + + MDScreen: + name: "scr 2" + + MDLabel: + text: "Screen 2" + halign: "center" + + MDNavigationDrawer: + id: nav_drawer + radius: (0, 16, 16, 0) + + ContentNavigationDrawer: + screen_manager: screen_manager + nav_drawer: nav_drawer + ''' - class TestNavigationDrawer(MDApp): - def build(self): - return Builder.load_string(KV) + class ContentNavigationDrawer(MDScrollView): + screen_manager = ObjectProperty() + nav_drawer = ObjectProperty() - TestNavigationDrawer().run() + class Example(MDApp): + def build(self): + self.theme_cls.primary_palette = "Orange" + self.theme_cls.theme_style = "Dark" + return Builder.load_string(KV) + + + Example().run() + + .. tab:: Declarative python styles + + .. code-block:: python + + from kivymd.app import MDApp + from kivymd.uix.label import MDLabel + from kivymd.uix.list import MDList, OneLineListItem + from kivymd.uix.navigationdrawer import MDNavigationLayout, MDNavigationDrawer + from kivymd.uix.screen import MDScreen + from kivymd.uix.screenmanager import MDScreenManager + from kivymd.uix.scrollview import MDScrollView + from kivymd.uix.toolbar import MDTopAppBar + + + class Example(MDApp): + def build(self): + self.theme_cls.primary_palette = "Orange" + self.theme_cls.theme_style = "Dark" + return ( + MDScreen( + MDTopAppBar( + pos_hint={"top": 1}, + elevation=4, + title="MDNavigationDrawer", + left_action_items=[["menu", lambda x: self.nav_drawer_open()]], + ), + MDNavigationLayout( + MDScreenManager( + MDScreen( + MDLabel( + text="Screen 1", + halign="center", + ), + name="scr 1", + ), + MDScreen( + MDLabel( + text="Screen 2", + halign="center", + ), + name="scr 2", + ), + id="screen_manager", + ), + MDNavigationDrawer( + MDScrollView( + MDList( + OneLineListItem( + text="Screen 1", + on_press=self.switch_screen, + ), + OneLineListItem( + text="Screen 2", + on_press=self.switch_screen, + ), + ), + ), + id="nav_drawer", + radius=(0, 16, 16, 0), + ), + id="navigation_layout", + ) + ) + ) + + def switch_screen(self, instance_list_item: OneLineListItem): + self.root.ids.navigation_layout.ids.screen_manager.current = { + "Screen 1": "scr 1", "Screen 2": "scr 2" + }[instance_list_item.text] + self.root.children[0].ids.nav_drawer.set_state("close") + + def nav_drawer_open(self): + nav_drawer = self.root.children[0].ids.nav_drawer + nav_drawer.set_state("open") + + + Example().run() """ __all__ = ( @@ -410,15 +551,15 @@ from kivy.properties import ( StringProperty, VariableListProperty, ) -from kivy.uix.floatlayout import FloatLayout from kivy.uix.screenmanager import ScreenManager -from kivy.uix.scrollview import ScrollView from kivymd import uix_path -from kivymd.uix.behaviors import FakeRectangularElevationBehavior, FocusBehavior +from kivymd.uix.behaviors.focus_behavior import FocusBehavior from kivymd.uix.boxlayout import MDBoxLayout from kivymd.uix.card import MDCard +from kivymd.uix.floatlayout import MDFloatLayout from kivymd.uix.list import MDList, OneLineAvatarIconListItem +from kivymd.uix.scrollview import MDScrollView from kivymd.uix.toolbar import MDTopAppBar with open( @@ -432,14 +573,19 @@ class NavigationDrawerContentError(Exception): pass -class MDNavigationLayout(FloatLayout): +class MDNavigationLayout(MDFloatLayout): + """ + For more information, see in the :class:`~kivymd.uix.floatlayout.MDFloatLayout` + class documentation. + """ + _scrim_color = ObjectProperty(None) _scrim_rectangle = ObjectProperty(None) _screen_manager = ObjectProperty(None) _navigation_drawer = ObjectProperty(None) - def __init__(self, **kwargs): - super().__init__(**kwargs) + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) self.bind(width=self.update_pos) def update_pos(self, instance_navigation_drawer, pos_x: float) -> None: @@ -514,6 +660,9 @@ class MDNavigationDrawerLabel(MDBoxLayout): """ Implements a label for a menu for :class:`~MDNavigationDrawer` class. + For more information, see in the :class:`~kivymd.uix.boxlayout.MDBoxLayout` + class documentation. + .. versionadded:: 1.0.0 .. code-block:: kv @@ -554,6 +703,9 @@ class MDNavigationDrawerDivider(MDBoxLayout): """ Implements a divider for a menu for :class:`~MDNavigationDrawer` class. + For more information, see in the :class:`~kivymd.uix.boxlayout.MDBoxLayout` + class documentation. + .. versionadded:: 1.0.0 .. code-block:: kv @@ -596,6 +748,9 @@ class MDNavigationDrawerHeader(MDBoxLayout): """ Implements a header for a menu for :class:`~MDNavigationDrawer` class. + For more information, see in the :class:`~kivymd.uix.boxlayout.MDBoxLayout` + class documentation. + .. versionadded:: 1.0.0 .. code-block:: kv @@ -737,6 +892,9 @@ class MDNavigationDrawerItem(OneLineAvatarIconListItem, FocusBehavior): """ Implements an item for the :class:`~MDNavigationDrawer` menu list. + For more information, see in the + :class:`~kivymd.uix.list.OneLineAvatarIconListItem` class documentation. + .. versionadded:: 1.0.0 .. code-block:: kv @@ -814,11 +972,14 @@ class MDNavigationDrawerItem(OneLineAvatarIconListItem, FocusBehavior): _drawer_menu = ObjectProperty() -class MDNavigationDrawerMenu(ScrollView): +class MDNavigationDrawerMenu(MDScrollView): """ Implements a scrollable list for menu items of the :class:`~MDNavigationDrawer` class. + For more information, see in the + :class:`~kivymd.uix.scrollview.MDScrollView` class documentation. + .. versionadded:: 1.0.0 .. code-block:: kv @@ -863,7 +1024,7 @@ class MDNavigationDrawerMenu(ScrollView): widget.text_color = widget._text_color -class MDNavigationDrawer(MDCard, FakeRectangularElevationBehavior): +class MDNavigationDrawer(MDCard): type = OptionProperty("modal", options=("standard", "modal")) """ Type of drawer. Modal type will be on top of screen. Standard type will be @@ -871,6 +1032,9 @@ class MDNavigationDrawer(MDCard, FakeRectangularElevationBehavior): :attr:`close_on_click` and :attr:`enable_swiping` to prevent closing drawer for standard type. + For more information, see in the :class:`~kivymd.uix.card.MDCard` + class documentation. + Standard -------- @@ -882,7 +1046,7 @@ class MDNavigationDrawer(MDCard, FakeRectangularElevationBehavior): .. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/navigation-drawer-standard.gif :align: center - Model + Modal ----- .. code-block:: kv @@ -1121,8 +1285,8 @@ class MDNavigationDrawer(MDCard, FakeRectangularElevationBehavior): and defaults to `0.2`. """ - def __init__(self, **kwargs): - super().__init__(**kwargs) + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) self.bind( open_progress=self.update_status, status=self.update_status, diff --git a/sbapp/kivymd/uix/navigationrail/__init__.py b/sbapp/kivymd/uix/navigationrail/__init__.py index 0ec0480..7d3ff75 100644 --- a/sbapp/kivymd/uix/navigationrail/__init__.py +++ b/sbapp/kivymd/uix/navigationrail/__init__.py @@ -2,5 +2,6 @@ from .navigationrail import ( MDNavigationRail, MDNavigationRailFabButton, + MDNavigationRailItem, MDNavigationRailMenuButton, ) diff --git a/sbapp/kivymd/uix/navigationrail/navigationrail.kv b/sbapp/kivymd/uix/navigationrail/navigationrail.kv index 9baf444..e2623bb 100644 --- a/sbapp/kivymd/uix/navigationrail/navigationrail.kv +++ b/sbapp/kivymd/uix/navigationrail/navigationrail.kv @@ -25,7 +25,7 @@ orientation: "vertical" size_hint: None, None - size: self.navigation_rail.width, "56dp" + size: self.navigation_rail.width if self.navigation_rail else 100, "56dp" RelativeLayout: id: container @@ -43,7 +43,8 @@ opacity: 0 md_bg_color: root.navigation_rail.ripple_color_item \ - if root.navigation_rail.ripple_color_item else \ + if root.navigation_rail and \ + root.navigation_rail.ripple_color_item else \ app.theme_cls.primary_color MDIcon: @@ -66,7 +67,8 @@ text_color: ( \ root.navigation_rail.icon_color_item_normal \ - if root.navigation_rail.icon_color_item_normal else \ + if root.navigation_rail \ + and root.navigation_rail.icon_color_item_normal else \ app.theme_cls.text_color \ ) \ if not root.active else \ @@ -79,7 +81,8 @@ container.height - \ ( \ (self.height + dp(4)) \ - if root.navigation_rail.type == "unselected" else \ + if root.navigation_rail and \ + root.navigation_rail.type == "unselected" else \ (self.height - dp(8)) \ ) @@ -102,17 +105,20 @@ RoundedRectangle: radius: [root._selected_region_width / 2,] \ - if root.navigation_rail.type == "unselected" else \ + if root.navigation_rail and \ + root.navigation_rail.type == "unselected" else \ [root._selected_region_width / 4,] size: root._selected_region_width, \ root._selected_region_width \ - if root.navigation_rail.type == "unselected" else \ + if root.navigation_rail and \ + root.navigation_rail.type == "unselected" else \ root._selected_region_width / 2 pos: self.center_x - self.width - dp(4), \ self.center_y - root._selected_region_width / 2 \ - if root.navigation_rail.type == "unselected" else \ + if root.navigation_rail and \ + root.navigation_rail.type == "unselected" else \ self.center_y - root._selected_region_width / 4 MDLabel: @@ -125,12 +131,16 @@ pos_hint: {"center_x": .5} y: "16" font_style: "Body2" - font_name: root.navigation_rail.font_name theme_text_color: "Custom" + font_name: + root.navigation_rail.font_name \ + if root.navigation_rail else \ + "Roboto" text_color: ( \ root.navigation_rail.text_color_item_normal \ - if root.navigation_rail.text_color_item_normal else \ + if root.navigation_rail and \ + root.navigation_rail.text_color_item_normal else \ app.theme_cls.text_color \ ) \ if not root.active else \ @@ -140,6 +150,8 @@ app.theme_cls.text_color \ ) opacity: - (0 if root.navigation_rail.type == "unselected" else 1) \ - if root.navigation_rail.type != "selected" else \ + (0 if root.navigation_rail and \ + root.navigation_rail.type == "unselected" else 1) \ + if root.navigation_rail and \ + root.navigation_rail.type != "selected" else \ (0 if not root.active else 1) diff --git a/sbapp/kivymd/uix/navigationrail/navigationrail.py b/sbapp/kivymd/uix/navigationrail/navigationrail.py index 881d6cd..42e2e24 100644 --- a/sbapp/kivymd/uix/navigationrail/navigationrail.py +++ b/sbapp/kivymd/uix/navigationrail/navigationrail.py @@ -29,45 +29,86 @@ Usage MDNavigationRailItem: -.. code-block:: python +.. tabs:: - from kivy.lang import Builder + .. tab:: Declarative KV style - from kivymd.app import MDApp + .. code-block:: python - KV = ''' + from kivy.lang import Builder + + from kivymd.app import MDApp + + KV = ''' + MDBoxLayout: + + MDNavigationRail: + + MDNavigationRailItem: + text: "Python" + icon: "language-python" + + MDNavigationRailItem: + text: "JavaScript" + icon: "language-javascript" + + MDNavigationRailItem: + text: "CPP" + icon: "language-cpp" + + MDNavigationRailItem: + text: "Git" + icon: "git" + + MDScreen: + ''' - MDBoxLayout: - - MDNavigationRail: - - MDNavigationRailItem: - text: "Python" - icon: "language-python" - - MDNavigationRailItem: - text: "JavaScript" - icon: "language-javascript" - - MDNavigationRailItem: - text: "CPP" - icon: "language-cpp" - - MDNavigationRailItem: - text: "Git" - icon: "git" - - MDScreen: - ''' + class Example(MDApp): + def build(self): + return Builder.load_string(KV) - class Example(MDApp): - def build(self): - return Builder.load_string(KV) + Example().run() + + .. tab:: Declarative python style + + .. code-block:: python + + from kivymd.app import MDApp + from kivymd.uix.boxlayout import MDBoxLayout + from kivymd.uix.navigationrail import MDNavigationRail, MDNavigationRailItem - Example().run() + class Example(MDApp): + def build(self): + self.theme_cls.theme_style = "Dark" + self.theme_cls.primary_palette = "Orange" + return ( + MDBoxLayout( + MDNavigationRail( + MDNavigationRailItem( + text="Python", + icon="language-python", + ), + MDNavigationRailItem( + text="JavaScript", + icon="language-javascript", + ), + MDNavigationRailItem( + text="CPP", + icon="language-cpp", + ), + MDNavigationRailItem( + text="Git", + icon="git", + ), + ) + ) + ) + + + Example().run() .. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/navigation-rail-usage.png :align: center @@ -75,202 +116,412 @@ Usage Example ======= -.. code-block:: python +.. tabs:: - from kivy.clock import Clock - from kivy.lang import Builder + .. tab:: Declarative KV and imperative python styles - from kivymd.app import MDApp - from kivymd.uix.behaviors import RoundedRectangularElevationBehavior - from kivymd.uix.boxlayout import MDBoxLayout - from kivymd.uix.button import MDFillRoundFlatIconButton - from kivymd.uix.label import MDLabel - from kivymd.uix.screen import MDScreen + .. code-block:: python - KV = ''' - #:import FadeTransition kivy.uix.screenmanager.FadeTransition + from kivy.clock import Clock + from kivy.lang import Builder + + from kivymd.app import MDApp + from kivymd.uix.behaviors import CommonElevationBehavior + from kivymd.uix.boxlayout import MDBoxLayout + from kivymd.uix.button import MDFillRoundFlatIconButton + from kivymd.uix.label import MDLabel + from kivymd.uix.screen import MDScreen + + KV = ''' + #:import FadeTransition kivy.uix.screenmanager.FadeTransition - - elevation: 3 - -height: "56dp" + + elevation: 3.5 + shadow_radius: 12 + shadow_softness: 4 + -height: "56dp" - - focus_color: "#e7e4c0" - unfocus_color: "#fffcf4" + + focus_color: "#e7e4c0" + unfocus_color: "#fffcf4" - MDScreen: + MDScreen: - MDNavigationLayout: + MDNavigationLayout: - ScreenManager: + ScreenManager: - MDScreen: + MDScreen: - MDBoxLayout: - orientation: "vertical" + MDBoxLayout: + orientation: "vertical" + + MDBoxLayout: + adaptive_height: True + md_bg_color: "#fffcf4" + padding: "12dp" + + MDLabel: + text: "12:00" + adaptive_height: True + pos_hint: {"center_y": .5} + + MDBoxLayout: + + MDNavigationRail: + id: navigation_rail + md_bg_color: "#fffcf4" + selected_color_background: "#e7e4c0" + ripple_color_item: "#e7e4c0" + on_item_release: app.switch_screen(*args) + + MDNavigationRailMenuButton: + on_release: nav_drawer.set_state("open") + + MDNavigationRailFabButton: + md_bg_color: "#b0f0d6" + + MDNavigationRailItem: + text: "Python" + icon: "language-python" + + MDNavigationRailItem: + text: "JavaScript" + icon: "language-javascript" + + MDNavigationRailItem: + text: "CPP" + icon: "language-cpp" + + MDNavigationRailItem: + text: "Swift" + icon: "language-swift" + + ScreenManager: + id: screen_manager + transition: + FadeTransition(duration=.2, clearcolor=app.theme_cls.bg_dark) + + MDNavigationDrawer: + id: nav_drawer + radius: (0, 16, 16, 0) + md_bg_color: "#fffcf4" + elevation: 4 + width: "240dp" + + MDNavigationDrawerMenu: MDBoxLayout: + orientation: "vertical" adaptive_height: True - md_bg_color: "#fffcf4" - padding: "12dp" + spacing: "12dp" + padding: "3dp", 0, 0, "12dp" - MDLabel: - text: "12:00" - adaptive_height: True - pos_hint: {"center_y": .5} + MDIconButton: + icon: "menu" - MDBoxLayout: + ExtendedButton: + text: "Compose" + icon: "pencil" - MDNavigationRail: - id: navigation_rail - md_bg_color: "#fffcf4" - selected_color_background: "#e7e4c0" - ripple_color_item: "#e7e4c0" - on_item_release: app.switch_screen(*args) + DrawerClickableItem: + text: "Python" + icon: "language-python" - MDNavigationRailMenuButton: - on_release: nav_drawer.set_state("open") + DrawerClickableItem: + text: "JavaScript" + icon: "language-javascript" - MDNavigationRailFabButton: - md_bg_color: "#b0f0d6" + DrawerClickableItem: + text: "CPP" + icon: "language-cpp" - MDNavigationRailItem: - text: "Python" - icon: "language-python" - - MDNavigationRailItem: - text: "JavaScript" - icon: "language-javascript" - - MDNavigationRailItem: - text: "CPP" - icon: "language-cpp" - - MDNavigationRailItem: - text: "Swift" - icon: "language-swift" - - ScreenManager: - id: screen_manager - transition: - FadeTransition(duration=.2, clearcolor=app.theme_cls.bg_dark) - - MDNavigationDrawer: - id: nav_drawer - radius: (0, 16, 16, 0) - md_bg_color: "#fffcf4" - elevation: 12 - width: "240dp" - - MDNavigationDrawerMenu: - - MDBoxLayout: - orientation: "vertical" - adaptive_height: True - spacing: "12dp" - padding: 0, 0, 0, "12dp" - - MDIconButton: - icon: "menu" - - ExtendedButton: - text: "Compose" - icon: "pencil" - - DrawerClickableItem: - text: "Python" - icon: "language-python" - - DrawerClickableItem: - text: "JavaScript" - icon: "language-javascript" - - DrawerClickableItem: - text: "CPP" - icon: "language-cpp" - - DrawerClickableItem: - text: "Swift" - icon: "language-swift" - ''' - - - class ExtendedButton( - RoundedRectangularElevationBehavior, MDFillRoundFlatIconButton - ): - ''' - Implements a button of type - `Extended FAB `_. - - .. rubric:: - Extended FABs help people take primary actions. - They're wider than FABs to accommodate a text label and larger target - area. - - This type of buttons is not yet implemented in the standard widget set - of the KivyMD library, so we will implement it ourselves in this class. - ''' - - def __init__(self, **kwargs): - super().__init__(**kwargs) - self.padding = "16dp" - Clock.schedule_once(self.set_spacing) - - def set_spacing(self, interval): - self.ids.box.spacing = "12dp" - - def set_radius(self, *args): - if self.rounded_button: - self._radius = self.radius = self.height / 4 - - - class Example(MDApp): - def build(self): - self.theme_cls.material_style = "M3" - self.theme_cls.primary_palette = "Orange" - return Builder.load_string(KV) - - def switch_screen( - self, instance_navigation_rail, instance_navigation_rail_item - ): - ''' - Called when tapping on rail menu items. Switches application screens. + DrawerClickableItem: + text: "Swift" + icon: "language-swift" ''' - self.root.ids.screen_manager.current = ( - instance_navigation_rail_item.icon.split("-")[1].lower() + + class ExtendedButton(MDFillRoundFlatIconButton, CommonElevationBehavior): + ''' + Implements a button of type + `Extended FAB `_. + + .. rubric:: + Extended FABs help people take primary actions. + They're wider than FABs to accommodate a text label and larger target + area. + + This type of buttons is not yet implemented in the standard widget set + of the KivyMD library, so we will implement it ourselves in this class. + ''' + + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + self.padding = "16dp" + Clock.schedule_once(self.set_spacing) + + def set_spacing(self, interval): + self.ids.box.spacing = "12dp" + + def set_radius(self, *args): + if self.rounded_button: + self._radius = self.radius = self.height / 4 + + + class Example(MDApp): + def build(self): + self.theme_cls.material_style = "M3" + self.theme_cls.primary_palette = "Orange" + return Builder.load_string(KV) + + def switch_screen( + self, instance_navigation_rail, instance_navigation_rail_item + ): + ''' + Called when tapping on rail menu items. Switches application screens. + ''' + + self.root.ids.screen_manager.current = ( + instance_navigation_rail_item.icon.split("-")[1].lower() + ) + + def on_start(self): + '''Creates application screens.''' + + navigation_rail_items = self.root.ids.navigation_rail.get_items()[:] + navigation_rail_items.reverse() + + for widget in navigation_rail_items: + name_screen = widget.icon.split("-")[1].lower() + screen = MDScreen( + name=name_screen, + md_bg_color="#edd769", + radius=[18, 0, 0, 0], + ) + box = MDBoxLayout(padding="12dp") + label = MDLabel( + text=name_screen.capitalize(), + font_style="H1", + halign="right", + adaptive_height=True, + shorten=True, + ) + box.add_widget(label) + screen.add_widget(box) + self.root.ids.screen_manager.add_widget(screen) + + + Example().run() + + .. tab:: Declarative python style + + .. code-block:: python + + from kivy.clock import Clock + from kivy.metrics import dp + + from kivymd.app import MDApp + from kivymd.uix.behaviors import CommonElevationBehavior + from kivymd.uix.boxlayout import MDBoxLayout + from kivymd.uix.button import MDFillRoundFlatIconButton, MDIconButton + from kivymd.uix.label import MDLabel + from kivymd.uix.navigationdrawer import ( + MDNavigationDrawerItem, + MDNavigationLayout, + MDNavigationDrawer, + MDNavigationDrawerMenu, ) - - def on_start(self): - '''Creates application screens.''' - - navigation_rail_items = self.root.ids.navigation_rail.get_items()[:] - navigation_rail_items.reverse() - - for widget in navigation_rail_items: - name_screen = widget.icon.split("-")[1].lower() - screen = MDScreen( - name=name_screen, - md_bg_color="#edd769", - radius=[18, 0, 0, 0], - ) - box = MDBoxLayout(padding="12dp") - label = MDLabel( - text=name_screen.capitalize(), - font_style="H1", - halign="right", - adaptive_height=True, - shorten=True, - ) - box.add_widget(label) - screen.add_widget(box) - self.root.ids.screen_manager.add_widget(screen) + from kivymd.uix.navigationrail import ( + MDNavigationRail, + MDNavigationRailMenuButton, + MDNavigationRailFabButton, + MDNavigationRailItem, + ) + from kivymd.uix.screen import MDScreen + from kivymd.uix.screenmanager import MDScreenManager - Example().run() + class DrawerClickableItem(MDNavigationDrawerItem): + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + self.focus_color = "#e7e4c0" + self.unfocus_color = self.theme_cls.bg_light + self.radius = 24 + + + class ExtendedButton(MDFillRoundFlatIconButton, CommonElevationBehavior): + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + self.padding = "16dp" + self.elevation = 3.5 + self.shadow_radius = 12 + self.shadow_softness = 4 + self.height = dp(56) + Clock.schedule_once(self.set_spacing) + + def set_spacing(self, interval): + self.ids.box.spacing = "12dp" + + def set_radius(self, *args): + if self.rounded_button: + self._radius = self.radius = self.height / 4 + + + class Example(MDApp): + def build(self): + self.theme_cls.material_style = "M3" + self.theme_cls.primary_palette = "Orange" + return MDScreen( + MDNavigationLayout( + MDScreenManager( + MDScreen( + MDBoxLayout( + MDBoxLayout( + MDLabel( + text="12:00", + adaptive_height=True, + pos_hint={"center_y": 0.5}, + ), + adaptive_height=True, + md_bg_color="#fffcf4", + padding="12dp", + ), + MDBoxLayout( + MDNavigationRail( + MDNavigationRailMenuButton( + on_release=self.open_nav_drawer, + ), + MDNavigationRailFabButton( + md_bg_color="#b0f0d6", + ), + MDNavigationRailItem( + text="Python", + icon="language-python", + ), + MDNavigationRailItem( + text="JavaScript", + icon="language-javascript", + ), + MDNavigationRailItem( + text="CPP", + icon="language-cpp", + ), + MDNavigationRailItem( + text="Swift", + icon="language-swift", + ), + id="navigation_rail", + md_bg_color="#fffcf4", + selected_color_background="#e7e4c0", + ripple_color_item="#e7e4c0", + ), + MDScreenManager( + id="screen_manager_content", + ), + id="root_box", + ), + id="box_rail", + orientation="vertical", + ), + id="box", + ), + id="screen", + ), + id="screen_manager", + ), + MDNavigationDrawer( + MDNavigationDrawerMenu( + MDBoxLayout( + MDIconButton( + icon="menu", + ), + ExtendedButton( + text="Compose", + icon="pencil", + ), + orientation="vertical", + adaptive_height=True, + spacing="12dp", + padding=("3dp", 0, 0, "12dp"), + ), + DrawerClickableItem( + text="Python", + icon="language-python", + ), + DrawerClickableItem( + text="JavaScript", + icon="language-javascript", + ), + DrawerClickableItem( + text="CPP", + icon="language-cpp", + ), + DrawerClickableItem( + text="Swift", + icon="language-swift", + ), + ), + id="nav_drawer", + radius=(0, 16, 16, 0), + elevation=4, + width="240dp", + ), + ) + + def switch_screen(self, *args, screen_manager_content=None): + ''' + Called when tapping on rail menu items. Switches application screens. + ''' + + instance_navigation_rail, instance_navigation_rail_item = args + screen_manager_content.current = ( + instance_navigation_rail_item.icon.split("-")[1].lower() + ) + + def open_nav_drawer(self, *args): + self.root.ids.nav_drawer.set_state("open") + + def on_start(self): + '''Creates application screens.''' + + screen_manager = self.root.ids.screen_manager + root_box = screen_manager.ids.screen.ids.box.ids.box_rail.ids.root_box + navigation_rail = root_box.ids.navigation_rail + screen_manager_content = root_box.ids.screen_manager_content + navigation_rail_items = navigation_rail.get_items()[:] + navigation_rail_items.reverse() + navigation_rail.bind( + on_item_release=lambda *args: self.switch_screen( + *args, screen_manager_content=screen_manager_content + ) + ) + + for widget in navigation_rail_items: + name_screen = widget.icon.split("-")[1].lower() + screen_manager_content.add_widget( + MDScreen( + MDBoxLayout( + MDLabel( + text=name_screen.capitalize(), + font_style="H1", + halign="right", + adaptive_height=True, + shorten=True, + ), + padding="12dp", + ), + name=name_screen, + md_bg_color="#edd769", + radius=[18, 0, 0, 0], + ), + ) + + + Example().run() .. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/navigation-rail-example.gif :align: center @@ -279,6 +530,7 @@ Example __all__ = ( "MDNavigationRail", + "MDNavigationRailItem", "MDNavigationRailFabButton", "MDNavigationRailMenuButton", ) @@ -305,12 +557,11 @@ from kivy.uix.behaviors import ButtonBehavior from kivymd import uix_path from kivymd.theming import ThemableBehavior -from kivymd.uix.behaviors import FakeRectangularElevationBehavior +from kivymd.uix.behaviors import ScaleBehavior from kivymd.uix.boxlayout import MDBoxLayout from kivymd.uix.button import MDFloatingActionButton, MDIconButton from kivymd.uix.card import MDCard from kivymd.uix.floatlayout import MDFloatLayout -from kivymd.uix.templates import ScaleWidget from kivymd.uix.widget import MDWidget with open( @@ -332,7 +583,7 @@ class PanelItems(MDBoxLayout): """Box for menu items.""" -class RippleWidget(MDWidget, ScaleWidget): +class RippleWidget(MDWidget, ScaleBehavior): """ Implements a background color for a menu item - (:class:`~MDNavigationRailItem`). @@ -561,7 +812,7 @@ class MDNavigationRailItem(ThemableBehavior, ButtonBehavior, MDBoxLayout): self.navigation_rail.dispatch("on_item_release", self) -class MDNavigationRail(MDCard, FakeRectangularElevationBehavior): +class MDNavigationRail(MDCard): """ :Events: :attr:`on_item_press` @@ -852,8 +1103,8 @@ class MDNavigationRail(MDCard, FakeRectangularElevationBehavior): and defaults to `'Roboto'`. """ - def __init__(self, **kwargs): - super().__init__(**kwargs) + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) Clock.schedule_once(self.set_pos_menu_fab_buttons) Clock.schedule_once(self.set_current_selected_item) self.register_event_type("on_item_press") diff --git a/sbapp/kivymd/uix/pickers/datepicker/datepicker.py b/sbapp/kivymd/uix/pickers/datepicker/datepicker.py index 50e80de..053afc8 100644 --- a/sbapp/kivymd/uix/pickers/datepicker/datepicker.py +++ b/sbapp/kivymd/uix/pickers/datepicker/datepicker.py @@ -11,65 +11,110 @@ Components/DatePicker .. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/picker-previous.png :align: center -.. warning:: The widget is under testing. Therefore, we would be grateful if - you would let us know about the bugs found. - .. rubric:: Usage -.. code-block:: python +.. tabs:: - from kivy.lang import Builder + .. tab:: Declarative KV style - from kivymd.app import MDApp - from kivymd.uix.pickers import MDDatePicker + .. code-block:: python - KV = ''' - MDFloatLayout: + from kivy.lang import Builder - MDTopAppBar: - title: "MDDatePicker" - pos_hint: {"top": 1} - elevation: 10 + from kivymd.app import MDApp + from kivymd.uix.pickers import MDDatePicker - MDRaisedButton: - text: "Open date picker" - pos_hint: {'center_x': .5, 'center_y': .5} - on_release: app.show_date_picker() - ''' + KV = ''' + MDFloatLayout: - - class Test(MDApp): - def build(self): - return Builder.load_string(KV) - - def on_save(self, instance, value, date_range): - ''' - Events called when the "OK" dialog box button is clicked. - - :type instance: ; - - :param value: selected date; - :type value: ; - - :param date_range: list of 'datetime.date' objects in the selected range; - :type date_range: ; + MDRaisedButton: + text: "Open date picker" + pos_hint: {'center_x': .5, 'center_y': .5} + on_release: app.show_date_picker() ''' - print(instance, value, date_range) - def on_cancel(self, instance, value): - '''Events called when the "CANCEL" dialog box button is clicked.''' + class Test(MDApp): + def build(self): + self.theme_cls.theme_style = "Dark" + self.theme_cls.primary_palette = "Orange" + return Builder.load_string(KV) - def show_date_picker(self): - date_dialog = MDDatePicker() - date_dialog.bind(on_save=self.on_save, on_cancel=self.on_cancel) - date_dialog.open() + def on_save(self, instance, value, date_range): + ''' + Events called when the "OK" dialog box button is clicked. + + :type instance: ; + :param value: selected date; + :type value: ; + :param date_range: list of 'datetime.date' objects in the selected range; + :type date_range: ; + ''' + + print(instance, value, date_range) + + def on_cancel(self, instance, value): + '''Events called when the "CANCEL" dialog box button is clicked.''' + + def show_date_picker(self): + date_dialog = MDDatePicker() + date_dialog.bind(on_save=self.on_save, on_cancel=self.on_cancel) + date_dialog.open() - Test().run() + Test().run() + + .. tab:: Declarative python style + + .. code-block:: python + + from kivymd.app import MDApp + from kivymd.uix.button import MDRaisedButton + from kivymd.uix.pickers import MDDatePicker + from kivymd.uix.screen import MDScreen -.. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/MDDatePicker.gif + class Test(MDApp): + def build(self): + self.theme_cls.theme_style = "Dark" + self.theme_cls.primary_palette = "Orange" + return ( + MDScreen( + MDRaisedButton( + text="Open data picker", + pos_hint={'center_x': .5, 'center_y': .5}, + on_release=self.show_date_picker, + ) + ) + ) + + def on_save(self, instance, value, date_range): + ''' + Events called when the "OK" dialog box button is clicked. + + :type instance: ; + + :param value: selected date; + :type value: ; + + :param date_range: list of 'datetime.date' objects in the selected range; + :type date_range: ; + ''' + + print(instance, value, date_range) + + def on_cancel(self, instance, value): + '''Events called when the "CANCEL" dialog box button is clicked.''' + + def show_date_picker(self, *args): + date_dialog = MDDatePicker() + date_dialog.bind(on_save=self.on_save, on_cancel=self.on_cancel) + date_dialog.open() + + + Test().run() + +.. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/MDDatePicker.png :align: center Open date dialog with the specified date @@ -81,7 +126,7 @@ Open date dialog with the specified date date_dialog = MDDatePicker(year=1983, month=4, day=12) date_dialog.open() -.. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/previous-date.png +.. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/specified-date.png :align: center Interval date @@ -94,12 +139,16 @@ that are not included in this range will have the status `disabled`. def show_date_picker(self): date_dialog = MDDatePicker( - min_date=datetime.date(2021, 2, 15), - max_date=datetime.date(2021, 3, 27), + min_date=datetime.date.today(), + max_date=datetime.date( + datetime.date.today().year, + datetime.date.today().month, + datetime.date.today().day + 2, + ), ) date_dialog.open() -.. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/range-date.gif +.. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/range-date.png :align: center The range of available dates can be changed in the picker dialog: @@ -122,7 +171,7 @@ You can set the range of years using the :attr:`~kivymd.uix.picker.MDDatePicker. .. code-block:: python def show_date_picker(self): - date_dialog = MDDatePicker(min_year=2021, max_year=2030) + date_dialog = MDDatePicker(min_year=2022, max_year=2030) date_dialog.open() .. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/min-max-year-date.png @@ -141,6 +190,8 @@ Set and select a date range :align: center """ +from __future__ import annotations + __all__ = ("MDDatePicker", "BaseDialogPicker", "DatePickerInputField") import calendar @@ -152,7 +203,6 @@ from typing import Union from kivy import Logger from kivy.animation import Animation -from kivy.clock import Clock from kivy.lang import Builder from kivy.metrics import dp from kivy.properties import ( @@ -175,7 +225,7 @@ from kivymd.theming import ThemableBehavior, ThemeManager from kivymd.toast import toast from kivymd.uix.behaviors import ( CircularRippleBehavior, - FakeRectangularElevationBehavior, + CommonElevationBehavior, SpecificBackgroundColorBehavior, ) from kivymd.uix.boxlayout import MDBoxLayout @@ -194,7 +244,7 @@ with open( class BaseDialogPicker( BaseDialog, - FakeRectangularElevationBehavior, + CommonElevationBehavior, SpecificBackgroundColorBehavior, ): """ @@ -255,11 +305,11 @@ class BaseDialogPicker( primary_color = ColorProperty(None) """ - Background color of toolbar in (r, g, b, a) format. + Background color of toolbar in (r, g, b, a) or string format. .. code-block:: python - MDDatePicker(primary_color=get_color_from_hex("#72225b")) + MDDatePicker(primary_color="brown") .. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/primary-color-date.png :align: center @@ -270,13 +320,13 @@ class BaseDialogPicker( accent_color = ColorProperty(None) """ - Background color of calendar/clock face in (r, g, b, a) format. + Background color of calendar/clock face in (r, g, b, a) or string format. .. code-block:: python MDDatePicker( - primary_color=get_color_from_hex("#72225b"), - accent_color=get_color_from_hex("#5d1a4a"), + primary_color="brown", + accent_color="darkred", ) .. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/accent-color-date.png @@ -288,14 +338,15 @@ class BaseDialogPicker( selector_color = ColorProperty(None) """ - Background color of the selected day of the month or hour in (r, g, b, a) format. + Background color of the selected day of the month or hour in (r, g, b, a) + or string format. .. code-block:: python MDDatePicker( - primary_color=get_color_from_hex("#72225b"), - accent_color=get_color_from_hex("#5d1a4a"), - selector_color=get_color_from_hex("#e93f39"), + primary_color="brown", + accent_color="darkred", + selector_color="red", ) .. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/selector-color-date.png @@ -307,15 +358,15 @@ class BaseDialogPicker( text_toolbar_color = ColorProperty(None) """ - Color of labels for text on a toolbar in (r, g, b, a) format. + Color of labels for text on a toolbar in (r, g, b, a) or string format. .. code-block:: python MDDatePicker( - primary_color=get_color_from_hex("#72225b"), - accent_color=get_color_from_hex("#5d1a4a"), - selector_color=get_color_from_hex("#e93f39"), - text_toolbar_color=get_color_from_hex("#cccccc"), + primary_color="brown", + accent_color="darkred", + selector_color="red", + text_toolbar_color="lightgrey", ) .. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/text-toolbar-color-date.png @@ -327,16 +378,16 @@ class BaseDialogPicker( text_color = ColorProperty(None) """ - Color of text labels in calendar/clock face in (r, g, b, a) format. + Color of text labels in calendar/clock face in (r, g, b, a) or string format. .. code-block:: python MDDatePicker( - primary_color=get_color_from_hex("#72225b"), - accent_color=get_color_from_hex("#5d1a4a"), - selector_color=get_color_from_hex("#e93f39"), - text_toolbar_color=get_color_from_hex("#cccccc"), - text_color=("#ffffff"), + primary_color="brown", + accent_color="darkred", + selector_color="red", + text_toolbar_color="lightgrey", + text_color="orange", ) .. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/text-color-date.png @@ -348,17 +399,18 @@ class BaseDialogPicker( text_current_color = ColorProperty(None) """ - Color of the text of the current day of the month/hour in (r, g, b, a) format. + Color of the text of the current day of the month/hour in (r, g, b, a) + or string format. .. code-block:: python MDDatePicker( - primary_color=get_color_from_hex("#72225b"), - accent_color=get_color_from_hex("#5d1a4a"), - selector_color=get_color_from_hex("#e93f39"), - text_toolbar_color=get_color_from_hex("#cccccc"), - text_color=("#ffffff"), - text_current_color=get_color_from_hex("#e93f39"), + primary_color="brown", + accent_color="darkred", + selector_color="red", + text_toolbar_color="lightgrey", + text_color="orange", + text_current_color="white", ) .. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/text-current-color-date.png @@ -375,13 +427,13 @@ class BaseDialogPicker( .. code-block:: python MDDatePicker( - primary_color=get_color_from_hex("#72225b"), - accent_color=get_color_from_hex("#5d1a4a"), - selector_color=get_color_from_hex("#e93f39"), - text_toolbar_color=get_color_from_hex("#cccccc"), - text_color=("#ffffff"), - text_current_color=get_color_from_hex("#e93f39"), - text_button_color=(1, 1, 1, .5), + primary_color="brown", + accent_color="darkred", + selector_color="red", + text_toolbar_color="lightgrey", + text_color="orange", + text_current_color="white", + text_button_color="lightgrey", ) .. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/text-button-color-date.png @@ -391,52 +443,124 @@ class BaseDialogPicker( and defaults to `None`. """ - input_field_background_color = ColorProperty(None) + input_field_background_color_normal = ColorProperty(None) """ - Background color of input fields in (r, g, b, a) format. + Background color normal of input fields in (r, g, b, a) or string format. + + .. versionadded:: 1.1.0 .. code-block:: python MDDatePicker( - primary_color=get_color_from_hex("#72225b"), - accent_color=get_color_from_hex("#5d1a4a"), - selector_color=get_color_from_hex("#e93f39"), - text_toolbar_color=get_color_from_hex("#cccccc"), - text_color=("#ffffff"), - text_current_color=get_color_from_hex("#e93f39"), - input_field_background_color=(1, 1, 1, 0.2), + primary_color="brown", + accent_color="darkred", + selector_color="red", + text_toolbar_color="lightgrey", + text_color="orange", + text_current_color="white", + text_button_color="lightgrey", + input_field_background_color_normal="coral", ) .. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/input-field-background-color-date.png :align: center - :attr:`input_field_background_color` is an :class:`~kivy.properties.ColorProperty` + :attr:`input_field_background_color_normal` is an :class:`~kivy.properties.ColorProperty` and defaults to `None`. """ + input_field_background_color_focus = ColorProperty(None) + """ + Background color normal of input fields in (r, g, b, a) or string format. + + .. versionadded:: 1.1.0 + + .. code-block:: python + + MDDatePicker( + primary_color="brown", + accent_color="darkred", + selector_color="red", + text_toolbar_color="lightgrey", + text_color="orange", + text_current_color="white", + text_button_color="lightgrey", + input_field_background_color_normal="coral", + input_field_background_color_focus="red", + ) + + .. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/input-field-background-color-focus-date.png + :align: center + + :attr:`input_field_background_color_focus` is an :class:`~kivy.properties.ColorProperty` + and defaults to `None`. + """ + + input_field_background_color = ColorProperty(None) + """ + .. deprecated:: 1.1.0 + Use :attr:`input_field_background_color_normal` instead. + """ + input_field_text_color = ColorProperty(None) """ - Text color of input fields in (r, g, b, a) format. + .. deprecated:: 1.1.0 + Use :attr:`input_field_text_color_normal` instead. + """ - Background color of input fields. + input_field_text_color_normal = ColorProperty(None) + """ + Text color normal of input fields in (r, g, b, a) or string format. + + .. versionadded:: 1.1.0 .. code-block:: python MDDatePicker( - primary_color=get_color_from_hex("#72225b"), - accent_color=get_color_from_hex("#5d1a4a"), - selector_color=get_color_from_hex("#e93f39"), - text_toolbar_color=get_color_from_hex("#cccccc"), - text_color=("#ffffff"), - text_current_color=get_color_from_hex("#e93f39"), - input_field_background_color=(1, 1, 1, 0.2), - input_field_text_color=(1, 1, 1, 1), + primary_color="brown", + accent_color="darkred", + selector_color="red", + text_toolbar_color="lightgrey", + text_color="orange", + text_current_color="white", + text_button_color="lightgrey", + input_field_background_color_normal="brown", + input_field_background_color_focus="red", + input_field_text_color_normal="white", ) - .. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/input-field-background-color-date.png + .. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/input-field-text-color-normal-date.png :align: center - :attr:`input_field_text_color` is an :class:`~kivy.properties.ColorProperty` + :attr:`input_field_text_color_normal` is an :class:`~kivy.properties.ColorProperty` + and defaults to `None`. + """ + + input_field_text_color_focus = ColorProperty(None) + """ + Text color focus of input fields in (r, g, b, a) or string format. + + .. versionadded:: 1.1.0 + + .. code-block:: python + + MDDatePicker( + primary_color="brown", + accent_color="darkred", + selector_color="red", + text_toolbar_color="lightgrey", + text_color="orange", + text_current_color="white", + text_button_color="lightgrey", + input_field_background_color_normal="brown", + input_field_background_color_focus="red", + input_field_text_color_normal="white", + ) + + .. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/input-field-text-color-normal-date.png + :align: center + + :attr:`input_field_text_color_focus` is an :class:`~kivy.properties.ColorProperty` and defaults to `None`. """ @@ -447,16 +571,18 @@ class BaseDialogPicker( .. code-block:: python MDDatePicker( - primary_color=get_color_from_hex("#72225b"), - accent_color=get_color_from_hex("#5d1a4a"), - selector_color=get_color_from_hex("#e93f39"), - text_toolbar_color=get_color_from_hex("#cccccc"), - text_color=("#ffffff"), - text_current_color=get_color_from_hex("#e93f39"), - input_field_background_color=(1, 1, 1, 0.2), - input_field_text_color=(1, 1, 1, 1), - font_name="Weather.ttf", - + primary_color="brown", + accent_color="darkred", + selector_color="red", + text_toolbar_color="lightgrey", + text_color="orange", + text_current_color="white", + text_button_color="lightgrey", + input_field_background_color_normal="brown", + input_field_background_color_focus="red", + input_field_text_color_normal="white", + input_field_text_color_focus="lightgrey", + font_name="nasalization.ttf", ) .. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/font-name-date.png @@ -471,6 +597,20 @@ class BaseDialogPicker( self.register_event_type("on_save") self.register_event_type("on_cancel") + def on_input_field_background_color( + self, instance, value: str | list | tuple + ) -> None: + """For supported of current API.""" + + self.input_field_background_color_normal = value + + def on_input_field_text_color( + self, instance, value: str | list | tuple + ) -> None: + """For supported of current API.""" + + self.input_field_text_color_normal = value + def on_save(self, *args) -> None: """Events called when the "OK" dialog box button is clicked.""" @@ -606,6 +746,13 @@ class DatePickerDaySelectableItem( self.owner.set_selected_widget(self) + def on_touch_down(self, touch): + # If year_layout is active don't dispatch on_touch_down events, + # so date items don't consume touch. + if not self.owner.ids._year_layout.disabled: + return + super().on_touch_down(touch) + class DatePickerYearSelectableItem(RecycleDataViewBehavior, MDLabel): """Implements an item for a pick list of the year.""" @@ -661,7 +808,7 @@ class DatePickerYearSelectableItem(RecycleDataViewBehavior, MDLabel): class MDDatePicker(BaseDialogPicker): text_weekday_color = ColorProperty(None) """ - Text color of weekday names in (r, g, b, a) format. + Text color of weekday names in (r, g, b, a) or string format. .. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/md-date-picker-text-weekday-color.png :align: center @@ -1200,19 +1347,39 @@ class MDDatePicker(BaseDialogPicker): """Creates and returns a text field object used to enter dates.""" if issubclass(self.input_field_cls, MDTextField): + text_color_focus = ( + self.input_field_text_color_focus + if self.input_field_text_color_focus + else self.theme_cls.primary_color + ) + text_color_normal = ( + self.input_field_text_color_normal + if self.input_field_text_color_normal + else self.theme_cls.disabled_hint_text_color + ) + fill_color_focus = ( + self.input_field_background_color_focus + if self.input_field_background_color_focus + else self.theme_cls.bg_dark + ) + fill_color_normal = ( + self.input_field_background_color_normal + if self.input_field_background_color_normal + else self.theme_cls.bg_darkest + ) + field = self.input_field_cls( owner=self, helper_text=self.helper_text, - line_color_normal=self.theme_cls.divider_color, + fill_color_normal=fill_color_normal, + fill_color_focus=fill_color_focus, + hint_text_color_normal=text_color_normal, + hint_text_color_focus=text_color_focus, + text_color_normal=text_color_normal, + text_color_focus=text_color_focus, + line_color_focus=text_color_focus, + line_color_normal=text_color_normal, ) - field.color_mode = "custom" - field.line_color_focus = ( - self.theme_cls.primary_color - if not self.input_field_text_color - else self.input_field_text_color - ) - field.current_hint_text_color = field.line_color_focus - field._current_hint_text_color = field.line_color_focus return field else: raise TypeError( diff --git a/sbapp/kivymd/uix/pickers/timepicker/timepicker.py b/sbapp/kivymd/uix/pickers/timepicker/timepicker.py index 748637e..2b646d8 100644 --- a/sbapp/kivymd/uix/pickers/timepicker/timepicker.py +++ b/sbapp/kivymd/uix/pickers/timepicker/timepicker.py @@ -16,35 +16,73 @@ Components/TimePicker .. rubric:: Usage -.. code-block:: +.. tabs:: - from kivy.lang import Builder + .. tab:: Declarative KV style - from kivymd.app import MDApp - from kivymd.uix.pickers import MDTimePicker + .. code-block:: python - KV = ''' - MDFloatLayout: + from kivy.lang import Builder - MDRaisedButton: - text: "Open time picker" - pos_hint: {'center_x': .5, 'center_y': .5} - on_release: app.show_time_picker() - ''' + from kivymd.app import MDApp + from kivymd.uix.pickers import MDTimePicker + + KV = ''' + MDFloatLayout: + + MDRaisedButton: + text: "Open time picker" + pos_hint: {'center_x': .5, 'center_y': .5} + on_release: app.show_time_picker() + ''' - class Test(MDApp): - def build(self): - return Builder.load_string(KV) + class Test(MDApp): + def build(self): + self.theme_cls.theme_style = "Dark" + self.theme_cls.primary_palette = "Orange" + return Builder.load_string(KV) - def show_time_picker(self): - '''Open time picker dialog.''' + def show_time_picker(self): + '''Open time picker dialog.''' - time_dialog = MDTimePicker() - time_dialog.open() + time_dialog = MDTimePicker() + time_dialog.open() - Test().run() + Test().run() + + .. tab:: Declarative python style + + .. code-block:: python + + from kivymd.app import MDApp + from kivymd.uix.button import MDRaisedButton + from kivymd.uix.pickers import MDTimePicker + from kivymd.uix.screen import MDScreen + + + class Test(MDApp): + def build(self): + self.theme_cls.theme_style = "Dark" + self.theme_cls.primary_palette = "Orange" + return ( + MDScreen( + MDRaisedButton( + text="Open time picker", + pos_hint={'center_x': .5, 'center_y': .5}, + on_release=self.show_time_picker, + ) + ) + ) + + def show_time_picker(self, *args): + '''Open time picker dialog.''' + + MDTimePicker().open() + + + Test().run() .. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/MDTimePicker.png :align: center @@ -91,14 +129,14 @@ Use the :attr:`~MDTimePicker.set_time` method of the .. code-block:: python - time_dialog = MDTimePicker( - primary_color=get_color_from_hex("#72225b"), - accent_color=get_color_from_hex("#5d1a4a"), - text_button_color=(1, 1, 1, 1), - ) + MDTimePicker( + primary_color="brown", + accent_color="red", + text_button_color="white", + ).open() - .. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/time-picker-customization.png - :align: center +.. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/time-picker-customization.png + :align: center """ __all__ = ("MDTimePicker",) @@ -194,11 +232,11 @@ class TimeInputTextField(MDTextField): hour_regx = "^[0-9]$|^0[1-9]$|^1[0-2]$" minute_regx = "^[0-9]$|^0[0-9]$|^[1-5][0-9]$" - def __init__(self, **kwargs): - super().__init__(**kwargs) + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) Clock.schedule_once(self.set_text) self.register_event_type("on_select") - self.bind(text_color=self.setter("hint_text_color_normal")) + self.bind(text_color_focus=self.setter("hint_text_color_normal")) def validate_time(self, text) -> Union[None, re.Match]: reg = self.hour_regx if self.num_type == "hour" else self.minute_regx @@ -217,17 +255,20 @@ class TimeInputTextField(MDTextField): to somehow make them aligned. """ - if not self.text: - self.text = " " + def set_text(*args): + if not self.text: + self.text = " " - self._refresh_text(self.text) - max_size = max(self._lines_rects, key=lambda r: r.size[0]).size - dx = (self.width - max_size[0]) / 2.0 - dy = (self.height - max_size[1]) / 2.0 - self.padding = [dx, dy, dx, dy] + self._refresh_text(self.text) + max_size = max(self._lines_rects, key=lambda r: r.size[0]).size + dx = (self.width - max_size[0]) / 2.0 + dy = (self.height - max_size[1]) / 2.0 + self.padding = [dx, dy, dx, dy] - if len(self.text) > 1: - self.text = self.text.replace(" ", "") + if len(self.text) > 1: + self.text = self.text.replace(" ", "") + + Clock.schedule_once(set_text) def on_focus(self, *args) -> None: super().on_focus(*args) diff --git a/sbapp/kivymd/uix/progressbar/progressbar.py b/sbapp/kivymd/uix/progressbar/progressbar.py index e7baaf7..9cedee8 100644 --- a/sbapp/kivymd/uix/progressbar/progressbar.py +++ b/sbapp/kivymd/uix/progressbar/progressbar.py @@ -247,7 +247,7 @@ class MDProgressBar(ThemableBehavior, ProgressBar): Clock.schedule_once(self.check_size) def check_size(self, interval: Union[int, float]) -> None: - if self.size == [100, 100]: + if self.height == 100: if self.orientation == "horizontal": self.size_hint_y = None self.height = dp(4) diff --git a/sbapp/kivymd/uix/recyclegridlayout.py b/sbapp/kivymd/uix/recyclegridlayout.py index 8a45768..d3ed891 100644 --- a/sbapp/kivymd/uix/recyclegridlayout.py +++ b/sbapp/kivymd/uix/recyclegridlayout.py @@ -1,14 +1,14 @@ """ Components/RecycleGridLayout -===================== +============================ -:class:`~kivy.uix.recyclegridlayout.RecycleGridLayout` class equivalent. Simplifies working -with some widget properties. For example: +:class:`~kivy.uix.recyclegridlayout.RecycleGridLayout` class equivalent. +Simplifies working with some widget properties. For example: -GridLayout ----------- +RecycleGridLayout +----------------- -.. code-block:: +.. code-block:: kv RecycleGridLayout: size_hint_y: None @@ -22,9 +22,9 @@ GridLayout size: self.size MDRecycleGridLayout ------------- +------------------- -.. code-block:: +.. code-block:: kv MDRecycleGridLayout: adaptive_height: True @@ -86,7 +86,13 @@ Equivalent from kivy.uix.recyclegridlayout import RecycleGridLayout from kivymd.uix import MDAdaptiveWidget +from kivymd.uix.behaviors import DeclarativeBehavior -class MDRecycleGridLayout(RecycleGridLayout, MDAdaptiveWidget): - pass +class MDRecycleGridLayout( + DeclarativeBehavior, RecycleGridLayout, MDAdaptiveWidget +): + """ + Recycle grid layout layout class. For more information, see in the + :class:`~kivy.uix.recyclegridlayout.RecycleGridLayout` class documentation. + """ diff --git a/sbapp/kivymd/uix/recycleview.py b/sbapp/kivymd/uix/recycleview.py new file mode 100644 index 0000000..9792103 --- /dev/null +++ b/sbapp/kivymd/uix/recycleview.py @@ -0,0 +1,44 @@ +""" +Components/RecycleView +====================== + +.. versionadded:: 1.0.0 + +:class:`~kivy.uix.recycleview.RecycleView` class equivalent. Simplifies working +with some widget properties. For example: + +RecycleView +----------- + +.. code-block:: kv + + RecycleView: + + canvas: + Color: + rgba: app.theme_cls.primary_color + Rectangle: + pos: self.pos + size: self.size + +MDRecycleView +------------- + +.. code-block:: kv + + MDRecycleView: + md_bg_color: app.theme_cls.primary_color +""" + +__all__ = ("MDRecycleView",) + +from kivy.uix.recycleview import RecycleView + +from kivymd.uix.behaviors import DeclarativeBehavior + + +class MDRecycleView(DeclarativeBehavior, RecycleView): + """ + Recycle view class. For more information, see in the + :class:`~kivy.uix.recycleview.RecycleView` class documentation. + """ diff --git a/sbapp/kivymd/uix/refreshlayout/refreshlayout.py b/sbapp/kivymd/uix/refreshlayout/refreshlayout.py index 845644c..170b76e 100755 --- a/sbapp/kivymd/uix/refreshlayout/refreshlayout.py +++ b/sbapp/kivymd/uix/refreshlayout/refreshlayout.py @@ -7,12 +7,12 @@ Example .. code-block:: python - from kivymd.app import MDApp from kivy.clock import Clock from kivy.lang import Builder from kivy.factory import Factory from kivy.properties import StringProperty + from kivymd.app import MDApp from kivymd.uix.button import MDIconButton from kivymd.icon_definitions import md_icons from kivymd.uix.list import ILeftBodyTouch, OneLineIconListItem @@ -27,7 +27,7 @@ Example icon: root.icon - + MDBoxLayout: orientation: 'vertical' @@ -36,7 +36,7 @@ Example title: app.title md_bg_color: app.theme_cls.primary_color background_palette: 'Primary' - elevation: 10 + elevation: 4 left_action_items: [['menu', lambda x: x]] MDScrollViewRefreshLayout: @@ -112,10 +112,10 @@ from kivy.lang import Builder from kivy.metrics import dp from kivy.properties import ColorProperty, NumericProperty, ObjectProperty from kivy.uix.floatlayout import FloatLayout -from kivy.uix.scrollview import ScrollView from kivymd import uix_path from kivymd.theming import ThemableBehavior +from kivymd.uix.scrollview import MDScrollView with open( os.path.join(uix_path, "refreshlayout", "refreshlayout.kv"), @@ -150,7 +150,7 @@ class _RefreshScrollEffect(DampedScrollEffect): return False -class MDScrollViewRefreshLayout(ScrollView): +class MDScrollViewRefreshLayout(MDScrollView): root_layout = ObjectProperty() """ The spinner will be attached to this layout. @@ -159,22 +159,31 @@ class MDScrollViewRefreshLayout(ScrollView): and defaults to `None`. """ - def __init__(self, **kargs): - super().__init__(**kargs) + refresh_callback = ObjectProperty() + """ + The method that will be called at the on_touch_up event, + provided that the overscroll of the list has been registered. + + :attr:`refresh_callback` is a :class:`~kivy.properties.ObjectProperty` + and defaults to `None`. + """ + + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) self.effect_cls = _RefreshScrollEffect - self._work_spinnrer = False + self._work_spinner = False self._did_overscroll = False self.refresh_spinner = None def on_touch_up(self, *args): - if self._did_overscroll and not self._work_spinnrer: + if self._did_overscroll and not self._work_spinner: if self.refresh_callback: self.refresh_callback() if not self.refresh_spinner: self.refresh_spinner = RefreshSpinner(_refresh_layout=self) self.root_layout.add_widget(self.refresh_spinner) self.refresh_spinner.start_anim_spinner() - self._work_spinnrer = True + self._work_spinner = True self._did_overscroll = False return True @@ -219,5 +228,5 @@ class RefreshSpinner(ThemableBehavior, FloatLayout): spinner = self.ids.spinner spinner.size = (dp(30), dp(30)) spinner.opacity = 1 - self._refresh_layout._work_spinnrer = False + self._refresh_layout._work_spinner = False self._refresh_layout._did_overscroll = False diff --git a/sbapp/kivymd/uix/relativelayout.py b/sbapp/kivymd/uix/relativelayout.py index 96338a7..64f4658 100644 --- a/sbapp/kivymd/uix/relativelayout.py +++ b/sbapp/kivymd/uix/relativelayout.py @@ -2,13 +2,13 @@ Components/RelativeLayout ========================= -:class:`~kivy.uix.relativelayout.RelativeLayout` class equivalent. Simplifies working -with some widget properties. For example: +:class:`~kivy.uix.relativelayout.RelativeLayout` class equivalent. +Simplifies working with some widget properties. For example: RelativeLayout -------------- -.. code-block:: +.. code-block:: kv RelativeLayout: canvas: @@ -22,7 +22,7 @@ RelativeLayout MDRelativeLayout ---------------- -.. code-block:: +.. code-block:: kv MDRelativeLayout: radius: [25, ] @@ -32,7 +32,11 @@ MDRelativeLayout from kivy.uix.relativelayout import RelativeLayout from kivymd.uix import MDAdaptiveWidget +from kivymd.uix.behaviors import DeclarativeBehavior -class MDRelativeLayout(RelativeLayout, MDAdaptiveWidget): - pass +class MDRelativeLayout(DeclarativeBehavior, RelativeLayout, MDAdaptiveWidget): + """ + Relative layout class. For more information, see in the + :class:`~kivy.uix.relativelayout.RelativeLayout` class documentation. + """ diff --git a/sbapp/kivymd/uix/responsivelayout.py b/sbapp/kivymd/uix/responsivelayout.py new file mode 100644 index 0000000..a3025b7 --- /dev/null +++ b/sbapp/kivymd/uix/responsivelayout.py @@ -0,0 +1,189 @@ +""" +Components/ResponsiveLayout +=========================== + +.. versionadded:: 1.0.0 + +.. rubric:: Responsive design is a graphic user interface (GUI) design + approach used to create content that adjusts smoothly to various screen + sizes. + +.. raw:: html + +
+ +
+ +The :class:`~MDResponsiveLayout` class does not reorganize your UI. Its task +is to track the size of the application screen and, depending on this size, +the :class:`~MDResponsiveLayout` class selects which UI layout should be +displayed at the moment: mobile, tablet or desktop. Therefore, if you want to +have a responsive view some kind of layout in your application, you should +have three KV files with UI markup for three platforms. + +You need to set three parameters for the :class:`~MDResponsiveLayout` class +:attr:`~MDResponsiveLayout.mobile_view`, +:attr:`~MDResponsiveLayout.tablet_view` and +:attr:`~MDResponsiveLayout.desktop_view`. These should be Kivy or KivyMD +widgets. + +Usage responsive +---------------- + +.. code-block:: python + + from kivy.lang import Builder + + from kivymd.app import MDApp + from kivymd.uix.label import MDLabel + from kivymd.uix.responsivelayout import MDResponsiveLayout + from kivymd.uix.screen import MDScreen + + KV = ''' + + halign: "center" + + + + CommonComponentLabel: + text: "Mobile" + + + + CommonComponentLabel: + text: "Table" + + + + CommonComponentLabel: + text: "Desktop" + + + ResponsiveView: + ''' + + + class CommonComponentLabel(MDLabel): + pass + + + class MobileView(MDScreen): + pass + + + class TabletView(MDScreen): + pass + + + class DesktopView(MDScreen): + pass + + + class ResponsiveView(MDResponsiveLayout, MDScreen): + def __init__(self, **kw): + super().__init__(**kw) + self.mobile_view = MobileView() + self.tablet_view = TabletView() + self.desktop_view = DesktopView() + + + class Test(MDApp): + def build(self): + return Builder.load_string(KV) + + + Test().run() + +.. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/responsive-usage.gif + :align: center + +.. note:: Use common components for platform layouts (mobile, tablet, desktop views). + As shown in the example above, such a common component is the + `CommonComponentLabel` widget. + +Perhaps you expected more from the :class:`~MDResponsiveLayout` widget, but +even `Flutter` uses a similar approach to creating a responsive UI. + +You can also use the `commands `_ +provided to you by the developer tools to create a project with an responsive +design. +""" + +from kivy.event import EventDispatcher +from kivy.properties import ObjectProperty + +from kivymd.uix.controllers import WindowController + + +class MDResponsiveLayout(EventDispatcher, WindowController): + """ + :Events: + :attr:`on_change_screen_type` + Called when the screen type changes. + """ + + mobile_view = ObjectProperty() + """ + Mobile view. Must be a Kivy or KivyMD widget. + + :attr:`mobile_view` is an :class:`~kivy.properties.ObjectProperty` + and defaults to `None`. + """ + + tablet_view = ObjectProperty() + """ + Tablet view. Must be a Kivy or KivyMD widget. + + :attr:`tablet_view` is an :class:`~kivy.properties.ObjectProperty` + and defaults to `None`. + """ + + desktop_view = ObjectProperty() + """ + Desktop view. Must be a Kivy or KivyMD widget. + + :attr:`desktop_view` is an :class:`~kivy.properties.ObjectProperty` + and defaults to `None`. + """ + + _current_device_type = "" + + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + self.register_event_type("on_change_screen_type") + + def on_change_screen_type(self, *args): + """Called when the screen type changes.""" + + def on_size(self, *args) -> None: + """Called when the application screen size changes.""" + + super().on_size(*args) + self.set_screen() + + if self._current_device_type != self.real_device_type: + self._current_device_type = self.real_device_type + + def set_screen(self) -> None: + """ + Sets the screen according to the type of application screen size: + mobile/tablet or desktop view. + """ + + if self.real_device_type != self._current_device_type: + self.clear_widgets() + + if self.mobile_view and self.tablet_view and self.desktop_view: + if self.real_device_type == "mobile": + self.add_widget(self.mobile_view) + elif self.real_device_type == "tablet": + self.add_widget(self.tablet_view) + elif self.real_device_type == "desktop": + self.add_widget(self.desktop_view) + + self.dispatch("on_change_screen_type", self.real_device_type) diff --git a/sbapp/kivymd/uix/screen.py b/sbapp/kivymd/uix/screen.py index 40798cf..2e399ec 100644 --- a/sbapp/kivymd/uix/screen.py +++ b/sbapp/kivymd/uix/screen.py @@ -8,7 +8,7 @@ with some widget properties. For example: Screen ------ -.. code-block:: +.. code-block:: kv Screen: canvas: @@ -22,35 +22,56 @@ Screen MDScreen -------- -.. code-block:: +.. code-block:: kv MDScreen: radius: [25, 0, 0, 0] md_bg_color: app.theme_cls.primary_color """ -from kivy.properties import ObjectProperty +from kivy.properties import ListProperty, ObjectProperty from kivy.uix.screenmanager import Screen from kivymd.uix import MDAdaptiveWidget +from kivymd.uix.behaviors import DeclarativeBehavior from kivymd.uix.hero import MDHeroTo -class MDScreen(Screen, MDAdaptiveWidget): - hero_to = ObjectProperty() +class MDScreen(DeclarativeBehavior, Screen, MDAdaptiveWidget): """ - Must be a :class:`~kivymd.uix.hero.MDHeroTo` class. + Screen is an element intended to be used with a + :class:`~kivymd.uix.screenmanager.MDScreenManager`. For more information, + see in the :class:`~kivy.uix.screenmanager.Screen` class documentation. + """ + + hero_to = ObjectProperty(deprecated=True) + """ + Must be a :class:`~kivymd.uix.hero.MDHeroTo` class. + See the documentation of the `MDHeroTo `_ widget for more detailed information. - .. versionchanged:: 1.0.0 + .. deprecated:: 1.0.0 + Use attr:`heroes_to` attribute instead. :attr:`hero_to` is an :class:`~kivy.properties.ObjectProperty` and defaults to `None`. """ - def on_hero_to(self, screen, widget) -> None: + heroes_to = ListProperty() + """ + Must be a list of :class:`~kivymd.uix.hero.MDHeroTo` class. + + .. versionadded:: 1.0.0 + + :attr:`heroes_to` is an :class:`~kivy.properties.LiatProperty` + and defaults to `[]`. + """ + + def on_hero_to(self, screen, widget: MDHeroTo) -> None: + """Called when the value of the :attr:`hero_to` attribute changes.""" + if not isinstance(widget, MDHeroTo) or not issubclass( widget.__class__, MDHeroTo ): @@ -58,3 +79,4 @@ class MDScreen(Screen, MDAdaptiveWidget): f"The `{widget}` widget must be an `kivymd.uix.hero.MDHeroTo` " f"class or inherited from this class" ) + self.heroes_to = [widget] diff --git a/sbapp/kivymd/uix/screenmanager.py b/sbapp/kivymd/uix/screenmanager.py index 1e23b68..55b6c0e 100644 --- a/sbapp/kivymd/uix/screenmanager.py +++ b/sbapp/kivymd/uix/screenmanager.py @@ -5,37 +5,64 @@ Components/ScreenManager .. versionadded:: 1.0.0 :class:`~kivy.uix.screenmanager.ScreenManager` class equivalent. -If you want to use Hero animations you need to use :class:`~kivymd.uix.screenmanager.MDScreenManager` -not :class:`~kivy.uix.screenmanager.ScreenManager` class. +If you want to use Hero animations you need to use +:class:`~kivymd.uix.screenmanager.MDScreenManager` not +:class:`~kivy.uix.screenmanager.ScreenManager` class. """ +from kivy import Logger from kivy.clock import Clock from kivy.properties import ListProperty, StringProperty from kivy.uix.screenmanager import ScreenManager +from kivymd.uix.behaviors import DeclarativeBehavior from kivymd.uix.hero import MDHeroFrom -class MDScreenManager(ScreenManager): - current_hero = StringProperty(None) +class MDScreenManager(DeclarativeBehavior, ScreenManager): """ - The name of the current tag for the :class:`~kivymd.uix.hero.MDHeroFrom` and - :class:`~kivymd.uix.hero.MDHeroTo` objects that will be animated when + Screen manager. This is the main class that will control your + :class:`~kivymd.uix.screen.MDScreen` stack and memory. + + For more + information, see in the :class:`~kivy.uix.screenmanager.ScreenManager` + class documentation. + """ + + current_hero = StringProperty(None, deprecated=True) + """ + The name of the current tag for the :class:`~kivymd.uix.hero.MDHeroFrom` + and :class:`~kivymd.uix.hero.MDHeroTo` objects that will be animated when animating the transition between screens. + .. deprecated:: 1.1.0 + Use :attr:`current_heroes` attribute instead. + See the `Hero `_ - module documentation for more information about creating and using Hero animations. + module documentation for more information about creating and using Hero + animations. :attr:`current_hero` is an :class:`~kivy.properties.StringProperty` and defaults to `None`. """ + current_heroes = ListProperty() + """ + A list of names (tags) of heroes that need to be animated when moving + to the next screen. + + .. versionadded:: 1.1.0 + + :attr:`current_heroes` is an :class:`~kivy.properties.ListProperty` + and defaults to `[]`. + """ + # Collection of `MDHeroFrom` objects on all screens of the current # screen manager. _heroes_data = ListProperty() - def __init__(self, **kwargs): - super().__init__(**kwargs) + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) Clock.schedule_once(self.check_transition) def check_transition(self, *args) -> None: @@ -48,28 +75,48 @@ class MDScreenManager(ScreenManager): self.transition = MDSlideTransition() - def get_hero_from_widget(self) -> None: + def get_hero_from_widget(self) -> list: """ - Get an :class:`~kivymd.uix.hero.MDHeroTo` object with the - :attr:`~current_hero` tag. + Get a list of :class:`~kivymd.uix.hero.MDHeroFrom` objects according + to the tag names specified in the :attr:`~current_heroes` list. """ - hero_from_widget = None + hero_from_widget = [] - for hero_widget in self._heroes_data: - if isinstance(hero_widget, MDHeroFrom) or issubclass( - hero_widget.__class__, MDHeroFrom - ): - if hero_widget.tag == self.current_hero: - hero_from_widget = hero_widget - break + for name_hero in self.current_heroes: + for hero_widget in self._heroes_data: + if isinstance(hero_widget, MDHeroFrom) or issubclass( + hero_widget.__class__, MDHeroFrom + ): + if hero_widget.tag == name_hero: + hero_from_widget.append(hero_widget) return hero_from_widget + def on_current_hero(self, instance, value: str) -> None: + """ + Called when the value of the :attr:`current_hero` attribute changes. + """ + + Logger.warning( + "KivyMD: " + "`kivymd/uix/screenmanager.MDScreenManager.current_hero` " + "attribute is deprecated. " + "Use `kivymd/uix/screenmanager.MDScreenManager.current_heroes` " + "attribute instead." + ) + if value: + self.current_heroes = [value] + else: + self.current_heroes = [] + def add_widget(self, widget, *args, **kwargs): super().add_widget(widget, *args, **kwargs) Clock.schedule_once(lambda x: self._create_heroes_data(widget)) + # TODO: Add a method to delete an object from the arrt:`_heroes_data` + # collection when deleting an object using the `remove_widget` method. + def _create_heroes_data(self, widget): def find_hero_widget(child_widget): widget_hero = None diff --git a/sbapp/kivymd/uix/scrollview.py b/sbapp/kivymd/uix/scrollview.py new file mode 100644 index 0000000..0b00061 --- /dev/null +++ b/sbapp/kivymd/uix/scrollview.py @@ -0,0 +1,49 @@ +""" +Components/ScrollView +===================== + +.. versionadded:: 1.0.0 + +:class:`~kivy.uix.scrollview.ScrollView` class equivalent. Simplifies working +with some widget properties. For example: + +ScrollView +---------- + +.. code-block:: kv + + ScrollView: + + canvas: + Color: + rgba: app.theme_cls.primary_color + Rectangle: + pos: self.pos + size: self.size + +MDScrollView +------------ + +.. code-block:: kv + + MDScrollView: + md_bg_color: app.theme_cls.primary_color +""" + +__all__ = ("MDScrollView",) + +from kivy.uix.scrollview import ScrollView + +from kivymd.uix.behaviors import ( + DeclarativeBehavior, + SpecificBackgroundColorBehavior, +) + + +class MDScrollView( + DeclarativeBehavior, SpecificBackgroundColorBehavior, ScrollView +): + """ + ScrollView class. For more information, see in the + :class:`~kivy.uix.scrollview.ScrollView` class documentation. + """ diff --git a/sbapp/kivymd/uix/segmentedcontrol/segmentedcontrol.kv b/sbapp/kivymd/uix/segmentedcontrol/segmentedcontrol.kv index 9aed57f..3ac3656 100644 --- a/sbapp/kivymd/uix/segmentedcontrol/segmentedcontrol.kv +++ b/sbapp/kivymd/uix/segmentedcontrol/segmentedcontrol.kv @@ -15,8 +15,11 @@ pos_hint: {"center_y": .5} x: root._segment_switch_x md_bg_color: root.segment_color - elevation: 6 + elevation: 2 _radius: root.radius[0] - 4 + width: + segment_panel.width / segment_panel.children_number \ + - segment_panel.spacing SegmentPanel: id: segment_panel diff --git a/sbapp/kivymd/uix/segmentedcontrol/segmentedcontrol.py b/sbapp/kivymd/uix/segmentedcontrol/segmentedcontrol.py index 243ae9a..5e9c7f4 100644 --- a/sbapp/kivymd/uix/segmentedcontrol/segmentedcontrol.py +++ b/sbapp/kivymd/uix/segmentedcontrol/segmentedcontrol.py @@ -10,58 +10,77 @@ Components/SegmentedControl Usage ===== -.. code-block:: python +.. tabs:: - from kivy.lang import Builder + .. tab:: Declarative KV style - from kivymd.app import MDApp + .. code-block:: python + + from kivy.lang import Builder + + from kivymd.app import MDApp - KV = ''' - MDScreen: + KV = ''' + MDScreen: - MDSegmentedControl: - pos_hint: {"center_x": .5, "center_y": .5} + MDSegmentedControl: + pos_hint: {"center_x": .5, "center_y": .5} - MDSegmentedControlItem: - text: "Male" + MDSegmentedControlItem: + text: "Male" - MDSegmentedControlItem: - text: "Female" + MDSegmentedControlItem: + text: "Female" - MDSegmentedControlItem: - text: "All" - ''' + MDSegmentedControlItem: + text: "All" + ''' - class Test(MDApp): - def build(self): - return Builder.load_string(KV) + class Example(MDApp): + def build(self): + self.theme_cls.theme_style = "Dark" + self.theme_cls.primary_palette = "Orange" + return Builder.load_string(KV) - Test().run() + Example().run() -Or only in python code: + .. tab:: Declarative python style -.. code-block:: python + .. code-block:: python - from kivymd.app import MDApp - from kivymd.uix.screen import MDScreen - from kivymd.uix.segmentedcontrol import MDSegmentedControl, MDSegmentedControlItem + from kivymd.app import MDApp + from kivymd.uix.screen import MDScreen + from kivymd.uix.segmentedcontrol import ( + MDSegmentedControl, MDSegmentedControlItem + ) - class Test(MDApp): - def build(self): - screen = MDScreen() - segment_control = MDSegmentedControl(pos_hint={"center_x": .5, "center_y": .5}) - segment_control.add_widget(MDSegmentedControlItem(text="Male")) - segment_control.add_widget(MDSegmentedControlItem(text="Female")) - segment_control.add_widget(MDSegmentedControlItem(text="All")) - screen.add_widget(segment_control) - return screen + class Example(MDApp): + def build(self): + self.theme_cls.theme_style = "Dark" + self.theme_cls.primary_palette = "Orange" + return ( + MDScreen( + MDSegmentedControl( + MDSegmentedControlItem( + text="Male" + ), + MDSegmentedControlItem( + text="Female" + ), + MDSegmentedControlItem( + text="All" + ), + pos_hint={"center_x": 0.5, "center_y": 0.5} + ) + ) + ) - Test().run() + Example().run() .. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/md-segmented-control-usage.gif :align: center @@ -117,12 +136,22 @@ with open( class MDSegmentedControlItem(MDLabel): - """Implements a label to place on the :class:`~SegmentPanel` panel.""" + """ + Implements a label to place on the :class:`~SegmentPanel` panel. + + See :class:`~kivymd.uix.label.MDLabel` class documentation for more + information. + """ # TODO: Add an attribute for the color of the active segment label. -class MDSegmentedControl(ThemableBehavior, MDRelativeLayout): +class MDSegmentedControl(MDRelativeLayout, ThemableBehavior): """ + Implements a segmented control panel. + + Relative layout class. For more information, see in the + :class:`~kivy.uix.relativelayout.RelativeLayout` class documentation. + :Events: `on_active` Called when the segment is activated. @@ -135,7 +164,7 @@ class MDSegmentedControl(ThemableBehavior, MDRelativeLayout): .. code-block:: kv MDSegmentedControl: - md_bg_color: "#451938" + md_bg_color: "brown" .. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/md-segmented-control-md-bg-color.png :align: center @@ -151,8 +180,8 @@ class MDSegmentedControl(ThemableBehavior, MDRelativeLayout): .. code-block:: kv MDSegmentedControl: - md_bg_color: "#451938" - segment_color: "#e4514f" + md_bg_color: "brown" + segment_color: "red" .. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/md-segmented-control-segment-color.png :align: center @@ -160,8 +189,8 @@ class MDSegmentedControl(ThemableBehavior, MDRelativeLayout): .. code-block:: kv MDSegmentedControl: - md_bg_color: "#451938" - segment_color: "#e4514f" + md_bg_color: "brown" + segment_color: "red" MDSegmentedControlItem: text: "[color=fff]Male[/color]" @@ -196,9 +225,9 @@ class MDSegmentedControl(ThemableBehavior, MDRelativeLayout): .. code-block:: kv MDSegmentedControl: - md_bg_color: "#451938" - segment_color: "#e4514f" - separator_color: 1, 1, 1, 1 + md_bg_color: "brown" + segment_color: "red" + separator_color: "white" .. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/md-segmented-control-separator-color.png :align: center @@ -249,15 +278,12 @@ class MDSegmentedControl(ThemableBehavior, MDRelativeLayout): _segment_switch_x = NumericProperty(dp(4)) - def __init__(self, **kw): - super().__init__(**kw) + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) self.register_event_type("on_active") Clock.schedule_once(self.set_default_colors) Clock.schedule_once(self._remove_last_separator) - # FIXME: Sometimes this interval is not enough to get the width - # of the segment label textures. - Clock.schedule_once(self._set_width_segment_switch, 2.2) def set_default_colors(self, *args) -> None: """ @@ -313,6 +339,10 @@ class MDSegmentedControl(ThemableBehavior, MDRelativeLayout): self.ids.segment_panel.add_widget(widget) separator = MDSeparator(orientation="vertical") self.ids.segment_panel.add_widget(separator) + if not self.ids.segment_panel._started: + self.ids.segment_panel._started = True + else: + self.ids.segment_panel.children_number += 1 Clock.schedule_once( lambda x: self.update_separator_color(separator) ) @@ -326,15 +356,6 @@ class MDSegmentedControl(ThemableBehavior, MDRelativeLayout): self.current_active_segment = widget self.dispatch("on_active", widget) - def _set_width_segment_switch(self, *args): - """ - Sets the width of the switch. I think this is not done quite correctly. - """ - - self.ids.segment_switch.width = self.ids.segment_panel.children[ - 0 - ].width + dp(12) - def _remove_last_separator(self, *args): self.ids.segment_panel.remove_widget(self.ids.segment_panel.children[0]) @@ -350,3 +371,7 @@ class SegmentPanel(MDBoxLayout): Implements a panel for placing items - :class:`~MDSegmentedControlItem` for the :class:`~MDSegmentedControl` class. """ + + children_number = NumericProperty(1) + + _started = BooleanProperty(defaultvalue=False) diff --git a/sbapp/kivymd/uix/selection/selection.py b/sbapp/kivymd/uix/selection/selection.py index c5c9556..a82804f 100644 --- a/sbapp/kivymd/uix/selection/selection.py +++ b/sbapp/kivymd/uix/selection/selection.py @@ -50,6 +50,7 @@ Example with TwoLineAvatarListItem from kivy.animation import Animation from kivy.lang import Builder + from kivy.utils import get_color_from_hex from kivymd.app import MDApp from kivymd.uix.list import TwoLineAvatarListItem @@ -100,7 +101,7 @@ Example with TwoLineAvatarListItem class Example(MDApp): - overlay_color = "#6042e4" + overlay_color = get_color_from_hex("#6042e4") def build(self): return Builder.load_string(KV) @@ -156,7 +157,7 @@ Example with FitImage from kivy.properties import ColorProperty from kivymd.app import MDApp - from kivymd.utils.fitimage import FitImage + from kivymd.uix.fitimage import FitImage KV = ''' MDBoxLayout: @@ -446,7 +447,7 @@ class SelectionItem(ThemableBehavior, MDRelativeLayout, TouchBehavior): def get_progress_round_pos(self) -> tuple: return ( - self.center_x - self.progress_round_size / 2, + (self.pos[0] + self.width / 2) - self.progress_round_size / 2, self.center_y - self.progress_round_size / 2, ) diff --git a/sbapp/kivymd/uix/selectioncontrol/selectioncontrol.kv b/sbapp/kivymd/uix/selectioncontrol/selectioncontrol.kv index bfbfcda..3d3b015 100644 --- a/sbapp/kivymd/uix/selectioncontrol/selectioncontrol.kv +++ b/sbapp/kivymd/uix/selectioncontrol/selectioncontrol.kv @@ -1,3 +1,7 @@ +#:import get_color_from_hex kivy.utils.get_color_from_hex +#:import colors kivymd.color_definitions.colors + + canvas: Clear @@ -16,59 +20,126 @@ - color: 1, 1, 1, 1 - canvas: - Color: - rgba: self.color - Ellipse: - size: self.size - pos: self.pos + radius: self.width / 2 + ripple_scale: 2 + + ThumbIcon: + id: icon + font_size: "16sp" + theme_text_color: "Custom" + pos_hint: {"center_x": .5, "center_y": .5} canvas.before: Color: rgba: - self._track_color_disabled if self.disabled else \ ( \ - self._track_color_active \ - if self.active else self._track_color_normal \ + self.track_color_disabled \ + if self.track_color_disabled else \ + self.theme_cls.disabled_hint_text_color) \ + if self.disabled else \ + ( \ + ( \ + self.track_color_active \ + if self.track_color_active else \ + self.theme_cls.primary_color[:-1] + [.5]) \ + if self.active else \ + (self.track_color_inactive \ + if self.track_color_inactive else \ + self.theme_cls.disabled_hint_text_color) \ ) RoundedRectangle: size: (self.width + dp(14), dp(28)) \ if root.widget_style == "ios" else \ - (self.width - dp(8), dp(16)) + ( \ + (self.width - dp(8), dp(16)) \ + if app.theme_cls.material_style == "M2" else \ + (self.width + dp(16), dp(32)) \ + ) pos: (self.x - dp(2), self.center_y - dp(14)) \ if root.widget_style == "ios" else \ (self.x + dp(8), self.center_y - dp(8)) radius: - [dp(14)] if root.widget_style == "ios" else [dp(7)] + [dp(14)] \ + if root.widget_style == "ios" else \ + [dp(7) if app.theme_cls.material_style == "M2" else dp(16)] Color: rgba: ( \ - self.theme_cls.disabled_hint_text_color[:-1] + [.2] \ + self.theme_cls.disabled_hint_text_color \ if not root.active else (0, 0, 0, 0) \ ) \ - if root.widget_style == "ios" else (0, 0, 0, 0) - Line: - width: 1 + if root.widget_style == "ios" \ + or app.theme_cls.material_style == "M3" else \ + (0, 0, 0, 0) + SmoothLine: + width: + 1 \ + if root.widget_style == "ios" \ + or app.theme_cls.material_style == "M2" else \ + 1.4 rounded_rectangle: ( \ self.x - dp(2), self.center_y - dp(14), self.width + dp(14), \ dp(28), dp(14), dp(14), dp(14), dp(14), dp(28) \ ) \ if root.widget_style == "ios" else \ - (1, 1, 1, 1, 1, 1, 1, 1, 1) + ( \ + (1, 1, 1, 1, 1, 1, 1, 1, 1) \ + if app.theme_cls.material_style == "M2" else \ + ( \ + self.x + dp(8), self.center_y - dp(8), self.width + dp(16), \ + dp(32), dp(16), dp(16), dp(16), dp(16), dp(32) \ + ) + ) Thumb: id: thumb size_hint: None, None size: dp(24), dp(24) - pos: root.pos[0] + root._thumb_pos[0], root.pos[1] + root._thumb_pos[1] - color: - root.thumb_color_disabled if root.disabled else \ - (root.thumb_color_down if root.active else root.thumb_color) - elevation: 8 if root.active else 5 - on_release: setattr(root, "active", not root.active) + elevation: + (2.5 if root.active else 1) \ + if app.theme_cls.material_style != "M3" else \ + 0 + pos: + (root.pos[0] + root._thumb_pos[0], root.pos[1] + root._thumb_pos[1]) \ + if root.widget_style == "ios" \ + or app.theme_cls.material_style == "M2" else \ + ( \ + root.pos[0] + self.width + root._thumb_pos[0], \ + root.pos[1] + (root.height / 2 - self.height / 2) + root._thumb_pos[1] \ + ) + _no_ripple_effect: + True \ + if app.theme_cls.material_style == "M3" \ + and root.widget_style != "ios" else \ + False + md_bg_color: + ( \ + root.thumb_color_disabled \ + if root.thumb_color_disabled else \ + get_color_from_hex(colors["Gray"]["800"]) \ + ) \ + if root.disabled else \ + ( \ + (root.thumb_color_active \ + if root.thumb_color_active else \ + root.theme_cls.primary_color \ + ) \ + if root.active else \ + ( \ + root.thumb_color_inactive \ + if root.thumb_color_inactive else \ + get_color_from_hex(colors["Gray"]["50"] \ + ) \ + ) \ + ) + on_touch_down: + if self.collide_point(*args[1].pos) and not root.disabled: \ + root.on_thumb_down() + on_touch_up: + if self.collide_point(*args[1].pos) and not root.disabled: \ + setattr(root, "active", not root.active) diff --git a/sbapp/kivymd/uix/selectioncontrol/selectioncontrol.py b/sbapp/kivymd/uix/selectioncontrol/selectioncontrol.py index d0503af..031fd3a 100755 --- a/sbapp/kivymd/uix/selectioncontrol/selectioncontrol.py +++ b/sbapp/kivymd/uix/selectioncontrol/selectioncontrol.py @@ -51,9 +51,6 @@ MDCheckbox ``(dp(48), dp(48))``, but the ripple effect takes up all the available space. -.. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/checkbox-no-size.gif - :align: center - Control state ------------- @@ -148,6 +145,35 @@ MDSwitch .. Note:: Control state of :class:`~MDSwitch` same way as in :class:`~MDCheckbox`. + +MDSwitch in M3 style +-------------------- + +.. code-block:: python + + from kivy.lang import Builder + + from kivymd.app import MDApp + + KV = ''' + MDScreen: + + MDSwitch: + pos_hint: {'center_x': .5, 'center_y': .5} + active: True + ''' + + + class Test(MDApp): + def build(self): + self.theme_cls.material_style = "M3" + return Builder.load_string(KV) + + + Test().run() + +.. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/checkbox-m3.gif + :align: center """ __all__ = ("MDCheckbox", "MDSwitch") @@ -159,26 +185,18 @@ from kivy.clock import Clock from kivy.lang import Builder from kivy.metrics import dp, sp from kivy.properties import ( - AliasProperty, BooleanProperty, ColorProperty, ListProperty, - NumericProperty, - OptionProperty, StringProperty, ) -from kivy.uix.behaviors import ButtonBehavior, ToggleButtonBehavior +from kivy.uix.behaviors import ToggleButtonBehavior from kivy.uix.floatlayout import FloatLayout -from kivy.uix.widget import Widget -from kivy.utils import get_color_from_hex from kivymd import uix_path -from kivymd.color_definitions import colors from kivymd.theming import ThemableBehavior -from kivymd.uix.behaviors import ( - CircularRippleBehavior, - FakeCircularElevationBehavior, -) +from kivymd.uix.behaviors import CircularRippleBehavior, CommonElevationBehavior +from kivymd.uix.floatlayout import MDFloatLayout from kivymd.uix.label import MDIcon with open( @@ -233,30 +251,84 @@ class MDCheckbox(CircularRippleBehavior, ToggleButtonBehavior, MDIcon): and defaults to `'checkbox-marked-circle'`. """ - selected_color = ColorProperty(None) + color_active = ColorProperty(None) """ - Selected color in ``rgba`` format. + Color when the checkbox is in the active state. - :attr:`selected_color` is a :class:`~kivy.properties.ColorProperty` + .. versionadded:: 1.0.0 + + .. code-block:: kv + + MDCheckbox: + color_active: "red" + + .. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/checkbox-color-active.png + :align: center + + :attr:`color_active` is a :class:`~kivy.properties.ColorProperty` and defaults to `None`. """ - unselected_color = ColorProperty(None) + color_inactive = ColorProperty(None) """ - Unelected color in ``rgba`` format. + Color when the checkbox is in the inactive state. - :attr:`unselected_color` is a :class:`~kivy.properties.ColorProperty` + .. versionadded:: 1.0.0 + + .. code-block:: kv + + MDCheckbox: + color_inactive: "blue" + + .. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/checkbox-color-inactive.png + :align: center + + :attr:`color_inactive` is a :class:`~kivy.properties.ColorProperty` and defaults to `None`. """ disabled_color = ColorProperty(None) """ - Disabled color in ``rgba`` format. + Color when the checkbox is in the disabled state. + + .. code-block:: kv + + MDCheckbox: + disabled_color: "lightgrey" + disabled: True + active: True + + .. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/checkbox-disabled-color.png + :align: center :attr:`disabled_color` is a :class:`~kivy.properties.ColorProperty` and defaults to `None`. """ + # Deprecated property. + + selected_color = ColorProperty(None, deprecated=True) + """ + Color when the checkbox is in the active state. + + .. deprecated:: 1.0.0 + Use :attr:`color_active` instead. + + :attr:`selected_color` is a :class:`~kivy.properties.ColorProperty` + and defaults to `None`. + """ + + unselected_color = ColorProperty(None, deprecated=True) + """ + Color when the checkbox is in the inactive state. + + .. deprecated:: 1.0.0 + Use :attr:`color_inactive` instead. + + :attr:`unselected_color` is a :class:`~kivy.properties.ColorProperty` + and defaults to `None`. + """ + _current_color = ColorProperty([0.0, 0.0, 0.0, 0.0]) def __init__(self, **kwargs): @@ -265,10 +337,10 @@ class MDCheckbox(CircularRippleBehavior, ToggleButtonBehavior, MDIcon): font_size=sp(24), duration=0.1, t="out_quad" ) super().__init__(**kwargs) - self.selected_color = self.theme_cls.primary_color - self.unselected_color = self.theme_cls.secondary_text_color + self.color_active = self.theme_cls.primary_color + self.color_inactive = self.theme_cls.secondary_text_color self.disabled_color = self.theme_cls.divider_color - self._current_color = self.unselected_color + self._current_color = self.color_inactive self.check_anim_out.bind( on_complete=lambda *x: self.check_anim_in.start(self) ) @@ -278,27 +350,29 @@ class MDCheckbox(CircularRippleBehavior, ToggleButtonBehavior, MDIcon): radio_icon_normal=self.update_icon, radio_icon_down=self.update_icon, group=self.update_icon, - selected_color=self.update_color, - unselected_color=self.update_color, + color_active=self.update_color, + color_inactive=self.update_color, disabled_color=self.update_color, disabled=self.update_color, state=self.update_color, ) - self.theme_cls.bind(primary_color=self.update_primary_color) - self.theme_cls.bind(theme_style=self.update_primary_color) + self.theme_cls.bind( + theme_style=self.update_primary_color, + primary_color=self.update_primary_color, + ) self.update_icon() self.update_color() - def update_primary_color(self, instance, value): + def update_primary_color(self, instance, value) -> None: if value in ("Dark", "Light"): if not self.disabled: self.color = self.theme_cls.primary_color else: self.color = self.disabled_color else: - self.selected_color = value + self.color_active = value - def update_icon(self, *args): + def update_icon(self, *args) -> None: if self.state == "down": self.icon = ( self.radio_icon_down if self.group else self.checkbox_icon_down @@ -310,15 +384,15 @@ class MDCheckbox(CircularRippleBehavior, ToggleButtonBehavior, MDIcon): else self.checkbox_icon_normal ) - def update_color(self, *args): + def update_color(self, *args) -> None: if self.disabled: self._current_color = self.disabled_color elif self.state == "down": - self._current_color = self.selected_color + self._current_color = self.color_active else: - self._current_color = self.unselected_color + self._current_color = self.color_inactive - def on_state(self, *args): + def on_state(self, *args) -> None: if self.state == "down": self.check_anim_in.cancel(self) self.check_anim_out.start(self) @@ -333,22 +407,25 @@ class MDCheckbox(CircularRippleBehavior, ToggleButtonBehavior, MDIcon): self.update_icon() self.active = False - def on_active(self, *args): + def on_active(self, *args) -> None: self.state = "down" if self.active else "normal" -class Thumb( - FakeCircularElevationBehavior, - CircularRippleBehavior, - ButtonBehavior, - Widget, -): - ripple_scale = NumericProperty(2) +class ThumbIcon(MDIcon): """ - See :attr:`~kivymd.uix.behaviors.ripplebehavior.CommonRipple.ripple_scale`. + Implements icon for the :class:`~Thumb` widget. - :attr:`ripple_scale` is a :class:`~kivy.properties.NumericProperty` - and defaults to `2`. + .. versionadded:: 1.0.0 + """ + + +class Thumb( + CommonElevationBehavior, + CircularRippleBehavior, + MDFloatLayout, +): + """ + Implements a thumb for the :class:`~MDSwitch` widget. """ def _set_ellipse(self, instance, value): @@ -365,7 +442,7 @@ class Thumb( ) -class MDSwitch(ThemableBehavior, ButtonBehavior, FloatLayout): +class MDSwitch(ThemableBehavior, FloatLayout): active = BooleanProperty(False) """ Indicates if the switch is active or inactive. @@ -374,161 +451,309 @@ class MDSwitch(ThemableBehavior, ButtonBehavior, FloatLayout): and defaults to `False`. """ - _thumb_color = ColorProperty(get_color_from_hex(colors["Gray"]["50"])) - - def _get_thumb_color(self): - return self._thumb_color - - def _set_thumb_color(self, color, alpha=None): - if len(color) == 2: - self._thumb_color = get_color_from_hex(colors[color[0]][color[1]]) - if alpha: - self._thumb_color[3] = alpha - elif len(color) == 4: - self._thumb_color = color - - thumb_color = AliasProperty( - _get_thumb_color, _set_thumb_color, bind=["_thumb_color"] - ) + icon_active = StringProperty() """ - Get thumb color ``rgba`` format. + Thumb icon when the switch is in the active state (only M3 style). - :attr:`thumb_color` is an :class:`~kivy.properties.AliasProperty` - and property is readonly. + .. versionadded:: 1.0.0 + + .. code-block:: kv + + MDSwitch: + active: True + icon_active: "check" + + .. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/switch-icon-active.png + :align: center + + :attr:`icon_active` is a :class:`~kivy.properties.StringProperty` + and defaults to `''`. """ - _thumb_color_down = ColorProperty([1, 1, 1, 1]) - - def _get_thumb_color_down(self): - return self._thumb_color_down - - def _set_thumb_color_down(self, color, alpha=None): - if len(color) == 2: - self._thumb_color_down = get_color_from_hex( - colors[color[0]][color[1]] - ) - if alpha: - self._thumb_color_down[3] = alpha - else: - self._thumb_color_down[3] = 1 - elif len(color) == 4: - self._thumb_color_down = color - - _thumb_color_disabled = ColorProperty( - get_color_from_hex(colors["Gray"]["400"]) - ) - - thumb_color_disabled = get_color_from_hex(colors["Gray"]["800"]) + icon_inactive = StringProperty() """ - Get thumb color disabled ``rgba`` format. + Thumb icon when the switch is in an inactive state (only M3 style). - :attr:`thumb_color_disabled` is an :class:`~kivy.properties.AliasProperty` - and property is readonly. + .. versionadded:: 1.0.0 + + .. code-block:: kv + + MDSwitch: + icon_inactive: "close" + + .. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/switch-icon-inactive.png + :align: center + + :attr:`icon_inactive` is a :class:`~kivy.properties.StringProperty` + and defaults to `''`. """ - def _get_thumb_color_disabled(self): - return self._thumb_color_disabled - - def _set_thumb_color_disabled(self, color, alpha=None): - if len(color) == 2: - self._thumb_color_disabled = get_color_from_hex( - colors[color[0]][color[1]] - ) - if alpha: - self._thumb_color_disabled[3] = alpha - elif len(color) == 4: - self._thumb_color_disabled = color - - thumb_color_down = AliasProperty( - _get_thumb_color_disabled, - _set_thumb_color_disabled, - bind=["_thumb_color_disabled"], - ) + icon_active_color = ColorProperty(None) """ - Get thumb color down ``rgba`` format. + Thumb icon color when the switch is in the active state (only M3 style). - :attr:`thumb_color_down` is an :class:`~kivy.properties.AliasProperty` - and property is readonly. + .. versionadded:: 1.0.0 + + .. code-block:: kv + + MDSwitch: + active: True + icon_active: "check" + icon_active_color: "white" + + .. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/switch-icon-active-color.png + :align: center + + :attr:`icon_active_color` is a :class:`~kivy.properties.ColorProperty` + and defaults to `None`. """ - theme_thumb_color = OptionProperty("Primary", options=["Primary", "Custom"]) + icon_inactive_color = ColorProperty(None) """ - Thumb color scheme name + Thumb icon color when the switch is in an inactive state (only M3 style). - :attr:`theme_thumb_color` is an :class:`~kivy.properties.OptionProperty` - and defaults to `Primary`. + .. versionadded:: 1.0.0 + + .. code-block:: kv + + MDSwitch: + icon_inactive: "close" + icon_inactive_color: "grey" + + .. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/switch-icon-inactive-color.png + :align: center + + :attr:`icon_inactive_color` is a :class:`~kivy.properties.ColorProperty` + and defaults to `None`. """ - theme_thumb_down_color = OptionProperty( - "Primary", options=["Primary", "Custom"] - ) + thumb_color_active = ColorProperty(None) """ - Thumb Down color scheme name + The color of the thumb when the switch is active. - :attr:`theme_thumb_down_color` is an :class:`~kivy.properties.OptionProperty` - and defaults to `Primary`. + .. versionadded:: 1.0.0 + + .. code-block:: kv + + MDSwitch: + active: True + thumb_color_active: "brown" + + .. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/switch-thumb-color-active.png + :align: center + + :attr:`thumb_color_active` is an :class:`~kivy.properties.ColorProperty` + and default to `None`. + """ + + thumb_color_inactive = ColorProperty(None) + """ + The color of the thumb when the switch is inactive. + + .. versionadded:: 1.0.0 + + .. code-block:: kv + + MDSwitch: + thumb_color_inactive: "brown" + + .. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/switch-thumb-color-inactive.png + :align: center + + :attr:`thumb_color_inactive` is an :class:`~kivy.properties.ColorProperty` + and default to `None`. + """ + + thumb_color_disabled = ColorProperty(None) + """ + The color of the thumb when the switch is in the disabled state. + + .. code-block:: kv + + MDSwitch: + active: True + thumb_color_disabled: "brown" + disabled: True + + .. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/switch-thumb-color-disabled.png + :align: center + + :attr:`thumb_color_disabled` is an :class:`~kivy.properties.ColorProperty` + and default to `None`. + """ + + track_color_active = ColorProperty(None) + """ + The color of the track when the switch is active. + + .. code-block:: kv + + MDSwitch: + active: True + track_color_active: "red" + + .. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/switch-track-color-active.png + :align: center + + :attr:`track_color_active` is an :class:`~kivy.properties.ColorProperty` + and default to `None`. + """ + + track_color_inactive = ColorProperty(None) + """ + The color of the track when the switch is inactive. + + .. versionadded:: 1.0.0 + + .. code-block:: kv + + MDSwitch: + track_color_inactive: "red" + + .. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/switch-track-color-inactive.png + :align: center + + :attr:`track_color_inactive` is an :class:`~kivy.properties.ColorProperty` + and default to `None`. + """ + + track_color_disabled = ColorProperty(None) + """ + The color of the track when the switch is in the disabled state. + + .. code-block:: kv + + MDSwitch: + track_color_disabled: "lightgrey" + disabled: True + + .. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/switch-track-color-disabled.png + :align: center + + :attr:`track_color_disabled` is an :class:`~kivy.properties.ColorProperty` + and default to `None`. """ - _track_color_active = ColorProperty([0, 0, 0, 0]) - _track_color_normal = ColorProperty([0, 0, 0, 0]) - _track_color_disabled = ColorProperty([0, 0, 0, 0]) _thumb_pos = ListProperty([0, 0]) def __init__(self, **kwargs): super().__init__(**kwargs) - self.theme_cls.bind( - theme_style=self._set_colors, - primary_color=self._set_colors, - primary_palette=self._set_colors, - ) - self.bind(active=self._update_thumb_pos) - Clock.schedule_once(self._set_colors) + self.bind(icon_active=self.set_icon, icon_inactive=self.set_icon) self.size_hint = (None, None) self.size = (dp(36), dp(48)) + Clock.schedule_once(self._check_style) + Clock.schedule_once(lambda x: self._update_thumb_pos(animation=False)) + Clock.schedule_once(lambda x: self.on_active(self, self.active)) - def _set_colors(self, *args): - self._track_color_normal = self.theme_cls.disabled_hint_text_color - if self.theme_cls.theme_style == "Dark": + def set_icon(self, instance_switch, icon_value: str) -> None: + def set_icon(*args): + icon = icon_value if icon_value else "blank" + self.ids.thumb.ids.icon.icon = icon - if self.theme_thumb_down_color == "Primary": - self._track_color_active = self.theme_cls.primary_color - else: - self._track_color_active = self.thumb_color_down + Clock.schedule_once(set_icon, 0.2) - self._track_color_active[3] = 0.5 - self._track_color_disabled = get_color_from_hex("FFFFFF") - self._track_color_disabled[3] = 0.1 - - if self.theme_thumb_color == "Primary": - self.thumb_color = get_color_from_hex(colors["Gray"]["400"]) - - if self.theme_thumb_down_color == "Primary": - self.thumb_color_down = get_color_from_hex( - colors[self.theme_cls.primary_palette]["200"] + def on_active(self, instance_switch, active_value: bool) -> None: + if self.theme_cls.material_style == "M3" and self.widget_style != "ios": + size = ( + ( + (dp(16), dp(16)) + if not self.icon_inactive + else (dp(24), dp(24)) ) - else: - if self.theme_thumb_down_color == "Primary": - self._track_color_active = get_color_from_hex( - colors[self.theme_cls.primary_palette]["200"] + if not active_value + else (dp(24), dp(24)) + ) + icon = "blank" + color = (0, 0, 0, 0) + + if self.icon_active and active_value: + icon = self.icon_active + color = ( + self.icon_active_color + if self.icon_active_color + else self.theme_cls.text_color + ) + elif self.icon_inactive and not active_value: + icon = self.icon_inactive + color = ( + self.icon_inactive_color + if self.icon_inactive_color + else self.theme_cls.text_color + ) + + Animation(size=size, t="out_quad", d=0.2).start(self.ids.thumb) + Animation(color=color, t="out_quad", d=0.2).start( + self.ids.thumb.ids.icon + ) + self.set_icon(self, icon) + + self._update_thumb_pos() + + # FIXME: If you move the cursor from the switch during the + # `on_touch_down` event, the animation of returning the thumb to + # the previous size does not work. The following code fixes this. + def on_thumb_down(self) -> None: + """ + Called at the on_touch_down event of the :class:`~Thumb` object. + Indicates the state of the switch "on/off" by an animation of + increasing the size of the thumb. + """ + + if self.widget_style != "ios" and self.theme_cls.material_style == "M3": + if self.active: + size = (dp(28), dp(28)) + pos = ( + self.ids.thumb.pos[0] - dp(2), + self.ids.thumb.pos[1] - dp(1.8), ) else: - self._track_color_active = self.thumb_color_down - - self._track_color_active[3] = 0.5 - self._track_color_disabled = self.theme_cls.disabled_hint_text_color - - if self.theme_thumb_down_color == "Primary": - self.thumb_color_down = self.theme_cls.primary_color - - if self.theme_thumb_color == "Primary": - self.thumb_color = get_color_from_hex(colors["Gray"]["50"]) + size = (dp(26), dp(26)) + pos = ( + ( + self.ids.thumb.pos[0] - dp(5), + self.ids.thumb.pos[1] - dp(5), + ) + if not self.icon_inactive + else ( + self.ids.thumb.pos[0] + dp(1), + self.ids.thumb.pos[1] - dp(1), + ) + ) + Animation(size=size, pos=pos, t="out_quad", d=0.2).start( + self.ids.thumb + ) def _update_thumb_pos(self, *args, animation=True): if self.active: - _thumb_pos = (self.width - dp(14), self.height / 2 - dp(12)) + _thumb_pos = ( + self.width + - ( + dp(14) + if self.widget_style == "ios" + or self.theme_cls.material_style == "M2" + else dp(28) + ), + self.height / 2 + - ( + dp(12) + if self.widget_style == "ios" + or self.theme_cls.material_style == "M2" + else dp(16) + ), + ) else: - _thumb_pos = (0, self.height / 2 - dp(12)) + _thumb_pos = ( + 0 if not self.icon_inactive else dp(-14), + self.height / 2 + - ( + dp(12) + if self.widget_style == "ios" + or self.theme_cls.material_style == "M2" + else dp(16) + ), + ) Animation.cancel_all(self, "_thumb_pos") + if animation: Animation(_thumb_pos=_thumb_pos, duration=0.2, t="out_quad").start( self @@ -536,5 +761,6 @@ class MDSwitch(ThemableBehavior, ButtonBehavior, FloatLayout): else: self._thumb_pos = _thumb_pos - def on_size(self, *args): - self._update_thumb_pos(animation=False) + def _check_style(self, *args): + if self.widget_style == "ios" or self.theme_cls.material_style == "M2": + self.set_icon(self, "") diff --git a/sbapp/kivymd/uix/slider/slider.kv b/sbapp/kivymd/uix/slider/slider.kv index 9af6ce5..5332381 100644 --- a/sbapp/kivymd/uix/slider/slider.kv +++ b/sbapp/kivymd/uix/slider/slider.kv @@ -1,8 +1,9 @@ -#:import images_path kivymd.images_path #:import Thumb kivymd.uix.selectioncontrol.Thumb +#:import get_color_from_hex kivy.utils.get_color_from_hex +#:import colors kivymd.color_definitions.colors - + @@ -10,9 +11,32 @@ Clear Color: rgba: - self._track_color_disabled if self.disabled \ - else (self._track_color_active if self.active \ - else self._track_color_normal) + ( \ + self.track_color_disabled \ + if self.track_color_disabled else \ + self.theme_cls.disabled_hint_text_color) \ + if self.disabled else \ + ( \ + ( \ + self.track_color_active \ + if self.track_color_active else \ + ( \ + get_color_from_hex(colors["Gray"]["400"]) \ + if app.theme_cls.theme_style == "Light" else \ + (1, 1, 1, .3) \ + ) \ + ) \ + if self.active else \ + ( \ + self.track_color_inactive \ + if self.track_color_inactive else \ + ( \ + self.theme_cls.disabled_hint_text_color \ + if app.theme_cls.theme_style == "Light" else \ + get_color_from_hex(colors["Gray"]["800"]) \ + ) \ + ) \ + ) Rectangle: size: (self.width - self.padding * 2 - self._offset[0], dp(4)) if \ @@ -26,10 +50,16 @@ # If 0 draw circle Color: rgba: - (0, 0, 0, 0) if not self._is_off \ - else (self._track_color_disabled if self.disabled \ - else (self._track_color_active \ - if self.active else self._track_color_normal)) + (0, 0, 0, 0) if not self._is_off else \ + ( \ + self.track_color_disabled \ + if self.disabled and self.track_color_disabled else \ + ( \ + self.theme_cls.disabled_hint_text_color \ + if app.theme_cls.theme_style == "Light" else \ + get_color_from_hex(colors["Gray"]["800"]) \ + ) \ + ) Line: width: 2 circle: @@ -41,55 +71,86 @@ Color: rgba: (0, 0, 0, 0) if self._is_off \ - else (self.color if not self.disabled \ - else self._track_color_disabled) + else \ + ( \ + ( \ + self.color if self.color else \ + app.theme_cls.primary_color \ + ) \ + if not self.disabled else \ + ( \ + self.track_color_disabled \ + if self.track_color_disabled else \ + ( \ + (0, 0, 0, .26) \ + if app.theme_cls.theme_style == "Light" else (1, 1, 1, .3) \ + ) \ + ) \ + ) Rectangle: size: ((self.width - self.padding * 2) * self.value_normalized, sp(4)) \ - if root.orientation == "horizontal" \ - else \ + if root.orientation == "horizontal" else \ (sp(4), (self.height - self.padding * 2) * self.value_normalized) pos: (self.x + self.padding, self.center_y - dp(4)) \ - if self.orientation == "horizontal" \ - else (self.center_x - dp(4), self.y + self.padding) + if self.orientation == "horizontal" else \ + (self.center_x - dp(4), self.y + self.padding) Thumb: id: thumb size_hint: None, None size: - (dp(12), dp(12)) \ - if root.disabled \ - else \ - ((dp(24), dp(24)) \ - if root.active \ - else \ + (dp(12), dp(12)) if root.disabled else ((dp(24), dp(24)) \ + if root.active else \ (dp(16), dp(16))) pos: (root.value_pos[0] - dp(8), root.center_y - thumb.height / 2 - dp(2)) \ if root.orientation == "horizontal" \ else (root.center_x - thumb.width / 2 - dp(2), \ root.value_pos[1] - dp(8)) - color: - (0, 0, 0, 0) if root._is_off else (root._track_color_disabled \ - if root.disabled else root.color) - elevation: 0 if root._is_off else (4 if root.active else 2) + md_bg_color: + (0, 0, 0, 0) if root._is_off else \ + ( \ + ( \ + root.thumb_color_disabled \ + if root.thumb_color_disabled else \ + get_color_from_hex(colors["Gray"]["800"]) \ + ) \ + if root.disabled else \ + ( \ + (root.thumb_color_active \ + if root.thumb_color_active else \ + root.theme_cls.primary_color \ + ) \ + if root.active else \ + ( \ + root.thumb_color_inactive \ + if root.thumb_color_inactive else \ + root.theme_cls.primary_color \ + ) \ + ) \ + ) + elevation: 0 if root._is_off else (3 if root.active else 1) HintBoxContainer: id: hint_box size_hint: None, None - md_bg_color: root.hint_bg_color - elevation: 0 + md_bg_color: root.hint_bg_color if root.hint_bg_color else [0, 0, 0, 0] + elevation: 1.5 opacity: 1 if root.active else 0 radius: root.hint_radius padding: "6dp", "6dp", "6dp", "8dp" + shadow_color: + ([0, 0, 0, 0.6] if root.hint_bg_color else [0, 0, 0, 0]) \ + if root.active else \ + [0, 0, 0, 0] size: lbl_value.width + self.padding[0] * 2, \ lbl_value.height + self.padding[0] pos: (root.value_pos[0] - dp(9), root.center_y - hint_box.height / 2 + dp(30)) \ - if root.orientation == "horizontal" \ - else \ + if root.orientation == "horizontal" else \ (root.center_x - hint_box.width / 2 + dp(30), root.value_pos[1] - dp(8)) MDLabel: @@ -101,16 +162,8 @@ adaptive_size: True pos_hint: {"center_x": .5, "center_y": .5} text_color: - ( \ - root.color \ - if root.active \ - else (0, 0, 0, 0) \ - ) \ - if not root.hint_text_color \ - else \ - root.hint_text_color + app.theme_cls.primary_color \ + if not root.hint_text_color else root.hint_text_color text: str(root.value) \ - if isinstance(root.step, float) \ - else \ - str(int(root.value)) + if isinstance(root.step, float) else str(int(root.value)) diff --git a/sbapp/kivymd/uix/slider/slider.py b/sbapp/kivymd/uix/slider/slider.py index 5611d3e..152a602 100644 --- a/sbapp/kivymd/uix/slider/slider.py +++ b/sbapp/kivymd/uix/slider/slider.py @@ -10,70 +10,13 @@ Components/Slider .. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/slider.png :align: center - -With value hint ---------------- - -.. code-block:: python - - from kivy.lang import Builder - - from kivymd.app import MDApp - - KV = ''' - MDScreen - - MDSlider: - min: 0 - max: 100 - value: 40 - ''' - - - class Test(MDApp): - def build(self): - return Builder.load_string(KV) - - - Test().run() - -.. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/slider-1.gif - :align: center - -Without value hint ------------------- - -.. code-block:: kv - - MDSlider: - min: 0 - max: 100 - value: 40 - hint: False - -.. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/slider-2.gif - :align: center - -Without custom color --------------------- - -.. code-block:: kv - - MDSlider: - min: 0 - max: 100 - value: 40 - hint: False - color: app.theme_cls.accent_color - -.. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/slider-3.png - :align: center """ __all__ = ("MDSlider",) import os +from kivy.clock import Clock from kivy.lang import Builder from kivy.metrics import dp from kivy.properties import ( @@ -83,10 +26,8 @@ from kivy.properties import ( VariableListProperty, ) from kivy.uix.slider import Slider -from kivy.utils import get_color_from_hex from kivymd import uix_path -from kivymd.color_definitions import colors from kivymd.theming import ThemableBehavior with open( @@ -96,6 +37,11 @@ with open( class MDSlider(ThemableBehavior, Slider): + """ + Class for creating a Slider widget. See in the + :class:`~kivy.uix.slider.Slider` class documentation. + """ + active = BooleanProperty(False) """ If the slider is clicked. @@ -104,17 +50,50 @@ class MDSlider(ThemableBehavior, Slider): and defaults to `False`. """ + color = ColorProperty(None) + """ + Color slider. + + .. code-block:: kv + + MDSlider + color: "red" + + .. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/slide-color.png + :align: center + + :attr:`color` is an :class:`~kivy.properties.ColorProperty` + and defaults to `None`. + """ + hint = BooleanProperty(True) """ If True, then the current value is displayed above the slider. + .. code-block:: kv + + MDSlider + hint: True + + .. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/slide-hint.png + :align: center + :attr:`hint` is an :class:`~kivy.properties.BooleanProperty` and defaults to `True`. """ - hint_bg_color = ColorProperty([0, 0, 0, 0]) + hint_bg_color = ColorProperty(None) """ - Hint rectangle color in ``rgba`` format. + Hint rectangle color in (r.g.b.a) format. + + .. code-block:: kv + + MDSlider + hint: True + hint_bg_color: "red" + + .. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/slide-hint-bg-color.png + :align: center :attr:`hint_bg_color` is an :class:`~kivy.properties.ColorProperty` and defaults to `[0, 0, 0, 0]`. @@ -122,7 +101,17 @@ class MDSlider(ThemableBehavior, Slider): hint_text_color = ColorProperty(None) """ - Hint text color in ``rgba`` format. + Hint text color in (r.g.b.a) format. + + .. code-block:: kv + + MDSlider + hint: True + hint_bg_color: "red" + hint_text_color: "white" + + .. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/slide-hint-text-color.png + :align: center :attr:`hint_text_color` is an :class:`~kivy.properties.ColorProperty` and defaults to `None`. @@ -132,10 +121,132 @@ class MDSlider(ThemableBehavior, Slider): """ Hint radius. + .. code-block:: kv + + MDSlider + hint: True + hint_bg_color: "red" + hint_text_color: "white" + hint_radius: [6, 0, 6, 0] + + .. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/slide-hint-radius.png + :align: center + :attr:`hint_radius` is an :class:`~kivy.properties.VariableListProperty` and defaults to `[dp(4), dp(4), dp(4), dp(4)]`. """ + thumb_color_active = ColorProperty(None) + """ + The color of the thumb when the slider is active. + + .. versionadded:: 1.0.0 + + .. code-block:: kv + + MDSlider + thumb_color_active: "red" + + .. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/slide-thumb-color-active.png + :align: center + + :attr:`thumb_color_active` is an :class:`~kivy.properties.ColorProperty` + and default to `None`. + """ + + thumb_color_inactive = ColorProperty(None) + """ + The color of the thumb when the slider is inactive. + + .. versionadded:: 1.0.0 + + .. code-block:: kv + + MDSlider + thumb_color_inactive: "red" + + .. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/slide-thumb-color-inactive.png + :align: center + + :attr:`thumb_color_inactive` is an :class:`~kivy.properties.ColorProperty` + and default to `None`. + """ + + thumb_color_disabled = ColorProperty(None) + """ + The color of the thumb when the slider is in the disabled state. + + .. versionadded:: 1.0.0 + + .. code-block:: kv + + MDSlider + value: 55 + disabled: True + thumb_color_disabled: "red" + + .. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/slide-thumb-color-disabled.png + :align: center + + :attr:`thumb_color_disabled` is an :class:`~kivy.properties.ColorProperty` + and default to `None`. + """ + + track_color_active = ColorProperty(None) + """ + The color of the track when the slider is active. + + .. versionadded:: 1.0.0 + + .. code-block:: kv + + MDSlider + track_color_active: "red" + + .. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/slide-track-color-active.png + :align: center + + :attr:`track_color_active` is an :class:`~kivy.properties.ColorProperty` + and default to `None`. + """ + + track_color_inactive = ColorProperty(None) + """ + The color of the track when the slider is inactive. + + .. versionadded:: 1.0.0 + + .. code-block:: kv + + MDSlider + track_color_inactive: "red" + + .. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/slide-track-color-inactive.png + :align: center + + :attr:`track_color_inactive` is an :class:`~kivy.properties.ColorProperty` + and default to `None`. + """ + + track_color_disabled = ColorProperty(None) + """ + The color of the track when the slider is in the disabled state. + + .. versionadded:: 1.0.0 + + .. code-block:: kv + + MDSlider + disabled: True + track_color_disabled: "red" + + .. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/slide-track-color-disabled.png + :align: center + + :attr:`track_color_disabled` is an :class:`~kivy.properties.ColorProperty` + and default to `None`. + """ + show_off = BooleanProperty(True) """ Show the `'off'` ring when set to minimum value. @@ -144,40 +255,29 @@ class MDSlider(ThemableBehavior, Slider): and defaults to `True`. """ - color = ColorProperty([0, 0, 0, 0]) - """ - Color slider in ``rgba`` format. - - :attr:`color` is an :class:`~kivy.properties.ColorProperty` - and defaults to `None`. - """ - - _track_color_active = ColorProperty([0, 0, 0, 0]) - _track_color_normal = ColorProperty([0, 0, 0, 0]) - _track_color_disabled = ColorProperty([0, 0, 0, 0]) _thumb_pos = ListProperty([0, 0]) - _thumb_color_disabled = ColorProperty( - get_color_from_hex(colors["Gray"]["400"]) - ) - # Internal state of ring + # Internal state of ring. _is_off = BooleanProperty(False) - # Internal adjustment to reposition sliders for ring + # Internal adjustment to reposition sliders for ring. _offset = ListProperty((0, 0)) def __init__(self, **kwargs): super().__init__(**kwargs) - self.theme_cls.bind( - theme_style=self._set_colors, - primary_color=self._set_colors, - primary_palette=self._set_colors, - ) - self._set_colors() + Clock.schedule_once(self.set_thumb_icon) - def on_hint(self, instance, value): - if not value: - self.remove_widget(self.ids.hint_box) + def set_thumb_icon(self, *args) -> None: + self.ids.thumb.ids.icon.icon = "blank" - def on_value_normalized(self, *args): + def on_hint(self, instance, value) -> None: + def on_hint(*args): + if not value: + self.remove_widget(self.ids.hint_box) + + # Schedule using for declarative style. + # Otherwise get AttributeError exception. + Clock.schedule_once(on_hint) + + def on_value_normalized(self, *args) -> None: """ When the ``value == min`` set it to `'off'` state and make slider a ring. @@ -185,13 +285,13 @@ class MDSlider(ThemableBehavior, Slider): self._update_is_off() - def on_show_off(self, *args): + def on_show_off(self, *args) -> None: self._update_is_off() - def on__is_off(self, *args): + def on__is_off(self, *args) -> None: self._update_offset() - def on_active(self, *args): + def on_active(self, *args) -> None: self._update_offset() def on_touch_down(self, touch): @@ -213,26 +313,3 @@ class MDSlider(ThemableBehavior, Slider): def _update_is_off(self): self._is_off = self.show_off and (self.value_normalized == 0) - - def _set_colors(self, *args): - if self.theme_cls.theme_style == "Dark": - self._track_color_normal = get_color_from_hex("FFFFFF") - self._track_color_normal[3] = 0.3 - self._track_color_active = self._track_color_normal - self._track_color_disabled = self._track_color_normal - if self.color == [0, 0, 0, 0]: - self.color = get_color_from_hex( - colors[self.theme_cls.primary_palette]["200"] - ) - self.thumb_color_disabled = get_color_from_hex( - colors["Gray"]["800"] - ) - else: - self._track_color_normal = get_color_from_hex("000000") - self._track_color_normal[3] = 0.26 - self._track_color_active = get_color_from_hex("000000") - self._track_color_active[3] = 0.38 - self._track_color_disabled = get_color_from_hex("000000") - self._track_color_disabled[3] = 0.26 - if self.color == [0, 0, 0, 0]: - self.color = self.theme_cls.primary_color diff --git a/sbapp/kivymd/uix/sliverappbar/sliverappbar.kv b/sbapp/kivymd/uix/sliverappbar/sliverappbar.kv index fd7fe7f..6757d18 100644 --- a/sbapp/kivymd/uix/sliverappbar/sliverappbar.kv +++ b/sbapp/kivymd/uix/sliverappbar/sliverappbar.kv @@ -13,7 +13,7 @@ rgba: root.background_color \ if root.background_color else \ - get_color_from_hex(root.theme_cls.primary_color) + root.theme_cls.primary_color a: root._opacity Rectangle: pos: self.pos diff --git a/sbapp/kivymd/uix/sliverappbar/sliverappbar.py b/sbapp/kivymd/uix/sliverappbar/sliverappbar.py index 4591741..6316315 100644 --- a/sbapp/kivymd/uix/sliverappbar/sliverappbar.py +++ b/sbapp/kivymd/uix/sliverappbar/sliverappbar.py @@ -38,8 +38,8 @@ Example from kivy.lang.builder import Builder + from kivymd.app import MDApp from kivymd.uix.card import MDCard - from kivymd.uix.behaviors import RoundedRectangularElevationBehavior KV = ''' @@ -47,7 +47,6 @@ Example height: "86dp" padding: "4dp" radius: 12 - elevation: 4 FitImage: source: "avatar.jpg" @@ -95,8 +94,10 @@ Example ''' - class CardItem(MDCard, RoundedRectangularElevationBehavior): - pass + class CardItem(MDCard): + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + self.elevation = 3 class Example(MDApp): @@ -167,7 +168,7 @@ class MDSliverAppbarHeader(MDBoxLayout): pass -class MDSliverAppbar(ThemableBehavior, MDBoxLayout): +class MDSliverAppbar(MDBoxLayout, ThemableBehavior): """ MDSliverAppbar class. See module documentation for more information. @@ -192,7 +193,6 @@ class MDSliverAppbar(ThemableBehavior, MDBoxLayout): from kivymd.uix.card import MDCard from kivymd.uix.toolbar import MDTopAppBar - from kivymd.uix.behaviors import RoundedRectangularElevationBehavior KV = ''' #:import SliverToolbar __main__.SliverToolbar @@ -203,7 +203,6 @@ class MDSliverAppbar(ThemableBehavior, MDBoxLayout): height: "86dp" padding: "4dp" radius: 12 - elevation: 4 FitImage: source: "avatar.jpg" @@ -252,13 +251,16 @@ class MDSliverAppbar(ThemableBehavior, MDBoxLayout): ''' - class CardItem(MDCard, RoundedRectangularElevationBehavior): - pass + class CardItem(MDCard): + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + self.elevation = 3 class SliverToolbar(MDTopAppBar): def __init__(self, **kwargs): super().__init__(**kwargs) + self.shadow_color = (0, 0, 0, 0) self.type_height = "medium" self.headline_text = "Headline medium" self.left_action_items = [["arrow-left", lambda x: x]] @@ -383,8 +385,8 @@ class MDSliverAppbar(ThemableBehavior, MDBoxLayout): _scroll_was_moving = BooleanProperty(False) _last_scroll_y_pos = 0.0 - def __init__(self, **kwargs): - super().__init__(**kwargs) + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) self.register_event_type("on_scroll_content") def on_scroll_content( @@ -412,21 +414,27 @@ class MDSliverAppbar(ThemableBehavior, MDBoxLayout): ) -> None: """Called when a value is set to the :attr:`toolbar_cls` parameter.""" - # If an MDTopAppBar object is already in use, delete it - # before adding a new MDTopAppBar object. - for widget in self.ids.float_box.children: - if issubclass(widget.__class__, MDTopAppBar): - self.ids.float_box.remove_widget(widget) + def on_toolbar_cls(*args): + # If an MDTopAppBar object is already in use, delete it + # before adding a new MDTopAppBar object. + for widget in self.ids.float_box.children: + if issubclass(widget.__class__, MDTopAppBar): + self.ids.float_box.remove_widget(widget) - # Adding a custom MDTopAppBar object. - if issubclass(instance_toolbar_cls.__class__, MDTopAppBar): - instance_toolbar_cls.pos_hint = {"top": 1} - self.ids.float_box.add_widget(instance_toolbar_cls) - else: - raise MDSliverAppbarException( - "The `toolbar_cls` parameter must be an object of the " - "`kivymd.uix.toolbar.MDTopAppBar class`" - ) + # Adding a custom MDTopAppBar object. + if issubclass(instance_toolbar_cls.__class__, MDTopAppBar): + instance_toolbar_cls.pos_hint = {"top": 1} + instance_toolbar_cls.elevation = 0 + self.ids.float_box.add_widget(instance_toolbar_cls) + else: + raise MDSliverAppbarException( + "The `toolbar_cls` parameter must be an object of the " + "`kivymd.uix.toolbar.MDTopAppBar class`" + ) + + # Schedule using for declarative style. + # Otherwise get AttributeError exception. + Clock.schedule_once(on_toolbar_cls) def on_vbar(self) -> None: if not self.background_color: diff --git a/sbapp/kivymd/uix/snackbar/snackbar.kv b/sbapp/kivymd/uix/snackbar/snackbar.kv index 4dcef8b..557ceda 100644 --- a/sbapp/kivymd/uix/snackbar/snackbar.kv +++ b/sbapp/kivymd/uix/snackbar/snackbar.kv @@ -8,7 +8,7 @@ padding: "10dp", "10dp", "10dp", "10dp" md_bg_color: "323232" if not root.bg_color else root.bg_color radius: root.radius - elevation: 11 if root.padding else 0 + elevation: 4 if root.padding else 0 canvas: Color: diff --git a/sbapp/kivymd/uix/snackbar/snackbar.py b/sbapp/kivymd/uix/snackbar/snackbar.py index 5154441..c4fbdc9 100755 --- a/sbapp/kivymd/uix/snackbar/snackbar.py +++ b/sbapp/kivymd/uix/snackbar/snackbar.py @@ -25,7 +25,7 @@ Usage #:import Snackbar kivymd.uix.snackbar.Snackbar - Screen: + MDScreen: MDRaisedButton: text: "Create simple snackbar" @@ -149,7 +149,7 @@ Custom usage KV = ''' - Screen: + MDScreen: MDFloatingActionButton: id: button @@ -227,7 +227,7 @@ Custom Snackbar pos_hint: {'center_y': .5} - Screen: + MDScreen: MDRaisedButton: text: "SHOW" @@ -284,7 +284,6 @@ from kivy.properties import ( ) from kivymd import uix_path -from kivymd.uix.behaviors import FakeRectangularElevationBehavior from kivymd.uix.button import BaseButton from kivymd.uix.card import MDCard @@ -294,7 +293,7 @@ with open( Builder.load_string(kv_file.read()) -class BaseSnackbar(MDCard, FakeRectangularElevationBehavior): +class BaseSnackbar(MDCard): """ :Events: :attr:`on_open` diff --git a/sbapp/kivymd/uix/stacklayout.py b/sbapp/kivymd/uix/stacklayout.py index 4782302..8988e8b 100644 --- a/sbapp/kivymd/uix/stacklayout.py +++ b/sbapp/kivymd/uix/stacklayout.py @@ -8,7 +8,7 @@ with some widget properties. For example: StackLayout ----------- -.. code-block:: +.. code-block:: kv StackLayout: size_hint_y: None @@ -24,7 +24,7 @@ StackLayout MDStackLayout ------------- -.. code-block:: +.. code-block:: kv MDStackLayout: adaptive_height: True @@ -83,10 +83,16 @@ Equivalent size: self.minimum_size """ +__all__ = ("MDStackLayout",) + from kivy.uix.stacklayout import StackLayout from kivymd.uix import MDAdaptiveWidget +from kivymd.uix.behaviors import DeclarativeBehavior -class MDStackLayout(StackLayout, MDAdaptiveWidget): - pass +class MDStackLayout(DeclarativeBehavior, StackLayout, MDAdaptiveWidget): + """ + Stack layout class. For more information, see in the + :class:`~kivy.uix.stacklayout.StackLayout` class documentation. + """ diff --git a/sbapp/kivymd/uix/swiper/swiper.py b/sbapp/kivymd/uix/swiper/swiper.py index 4fd620b..b89e2e5 100644 --- a/sbapp/kivymd/uix/swiper/swiper.py +++ b/sbapp/kivymd/uix/swiper/swiper.py @@ -1,6 +1,6 @@ """ -Components/MDSwiper -=================== +Components/Swiper +================= .. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/mdswiper-preview.gif :align: center @@ -38,7 +38,7 @@ Example MDTopAppBar: id: toolbar title: "MDSwiper" - elevation: 10 + elevation: 4 pos_hint: {"top": 1} MDSwiper: @@ -142,7 +142,7 @@ Example MDTopAppBar: id: toolbar title: "MDSwiper" - elevation: 10 + elevation: 4 pos_hint: {"top": 1} MDSwiper: @@ -203,7 +203,6 @@ from kivy.animation import Animation from kivy.clock import Clock from kivy.core.window import Window from kivy.effects.dampedscroll import DampedScrollEffect -from kivy.event import EventDispatcher from kivy.lang.builder import Builder from kivy.properties import ( BooleanProperty, @@ -212,11 +211,11 @@ from kivy.properties import ( StringProperty, ) from kivy.uix.anchorlayout import AnchorLayout -from kivy.uix.boxlayout import BoxLayout -from kivy.uix.scrollview import ScrollView from kivy.utils import platform from kivymd import uix_path +from kivymd.uix.boxlayout import MDBoxLayout +from kivymd.uix.scrollview import MDScrollView with open( os.path.join(uix_path, "swiper", "swiper.kv"), encoding="utf-8" @@ -249,7 +248,7 @@ class _ItemsBox(AnchorLayout): ] -class MDSwiperItem(BoxLayout): +class MDSwiperItem(MDBoxLayout): """ :class:`MDSwiperItem` is a :class:`BoxLayout` but it's size is adjusted automatically. @@ -258,8 +257,8 @@ class MDSwiperItem(BoxLayout): _root = ObjectProperty() _selected = False - def __init__(self, **kwargs): - super().__init__(**kwargs) + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) Clock.schedule_once(self._set_size) Window.bind(on_resize=self._set_size) @@ -293,7 +292,7 @@ class MDSwiperItem(BoxLayout): anim.start(self) -class MDSwiper(ScrollView, EventDispatcher): +class MDSwiper(MDScrollView): items_spacing = NumericProperty("20dp") """ The space between each :class:`MDSwiperItem`. @@ -373,8 +372,8 @@ class MDSwiper(ScrollView, EventDispatcher): "on_swipe_right", ) - def __init__(self, **kwargs): - super().__init__(**kwargs) + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) self.register_event_type("on_swipe") self.register_event_type("on_pre_swipe") self.register_event_type("on_overswipe_right") diff --git a/sbapp/kivymd/uix/tab/tab.kv b/sbapp/kivymd/uix/tab/tab.kv index 824f82e..f219bc1 100644 --- a/sbapp/kivymd/uix/tab/tab.kv +++ b/sbapp/kivymd/uix/tab/tab.kv @@ -79,6 +79,10 @@ layout: layout size_hint: 1, None elevation: root.elevation + radius: root.radius + shadow_offset: root.shadow_offset + shadow_color: root.shadow_color + shadow_softness: root.shadow_softness height: root.tab_bar_height md_bg_color: self.theme_cls.primary_color \ diff --git a/sbapp/kivymd/uix/tab/tab.py b/sbapp/kivymd/uix/tab/tab.py index b78ffd6..816711e 100755 --- a/sbapp/kivymd/uix/tab/tab.py +++ b/sbapp/kivymd/uix/tab/tab.py @@ -25,12 +25,10 @@ content for the tab. class Tab(MDFloatLayout, MDTabsBase): '''Class implementing content for a tab.''' - content_text = StringProperty("") .. code-block:: kv - content_text MDLabel: text: root.content_text @@ -57,71 +55,139 @@ All tabs must be contained inside a :class:`~MDTabs` widget: Example with tab icon --------------------- -.. code-block:: python +.. tabs:: - from kivy.lang import Builder + .. tab:: Declarative KV and imperative python styles - from kivymd.app import MDApp - from kivymd.uix.tab import MDTabsBase - from kivymd.uix.floatlayout import MDFloatLayout - from kivymd.icon_definitions import md_icons + .. code-block:: python - KV = ''' - MDBoxLayout: - orientation: "vertical" + from kivy.lang import Builder - MDTopAppBar: - title: "Example Tabs" + from kivymd.app import MDApp + from kivymd.uix.tab import MDTabsBase + from kivymd.uix.floatlayout import MDFloatLayout + from kivymd.icon_definitions import md_icons - MDTabs: - id: tabs - on_tab_switch: app.on_tab_switch(*args) + KV = ''' + MDBoxLayout: + orientation: "vertical" + + MDTopAppBar: + title: "Example Tabs" + + MDTabs: + id: tabs + on_tab_switch: app.on_tab_switch(*args) - + - MDIconButton: - id: icon - icon: root.icon - user_font_size: "48sp" - pos_hint: {"center_x": .5, "center_y": .5} - ''' - - - class Tab(MDFloatLayout, MDTabsBase): - '''Class implementing content for a tab.''' - - - class Example(MDApp): - icons = list(md_icons.keys())[15:30] - - def build(self): - return Builder.load_string(KV) - - def on_start(self): - for tab_name in self.icons: - self.root.ids.tabs.add_widget(Tab(icon=tab_name)) - - def on_tab_switch( - self, instance_tabs, instance_tab, instance_tab_label, tab_text - ): + MDIconButton: + id: icon + icon: root.icon + icon_size: "48sp" + pos_hint: {"center_x": .5, "center_y": .5} ''' - Called when switching tabs. - - :type instance_tabs: ; - :param instance_tab: <__main__.Tab object>; - :param instance_tab_label: ; - :param tab_text: text or name icon of tab; - ''' - # get the tab icon. - count_icon = instance_tab.icon - # print it on shell/bash. - print(f"Welcome to {count_icon}' tab'") - Example().run() + class Tab(MDFloatLayout, MDTabsBase): + '''Class implementing content for a tab.''' + class Example(MDApp): + icons = list(md_icons.keys())[15:30] + + def build(self): + self.theme_cls.theme_style = "Dark" + self.theme_cls.primary_palette = "Orange" + return Builder.load_string(KV) + + def on_start(self): + for tab_name in self.icons: + self.root.ids.tabs.add_widget(Tab(icon=tab_name)) + + def on_tab_switch( + self, instance_tabs, instance_tab, instance_tab_label, tab_text + ): + ''' + Called when switching tabs. + + :type instance_tabs: ; + :param instance_tab: <__main__.Tab object>; + :param instance_tab_label: ; + :param tab_text: text or name icon of tab; + ''' + + count_icon = instance_tab.icon # get the tab icon + print(f"Welcome to {count_icon}' tab'") + + + Example().run() + + .. tab:: Declarative python styles + + .. code-block:: python + + from kivymd.app import MDApp + from kivymd.uix.boxlayout import MDBoxLayout + from kivymd.uix.button import MDIconButton + from kivymd.uix.tab import MDTabsBase, MDTabs + from kivymd.uix.floatlayout import MDFloatLayout + from kivymd.icon_definitions import md_icons + from kivymd.uix.toolbar import MDTopAppBar + + + class Tab(MDFloatLayout, MDTabsBase): + '''Class implementing content for a tab.''' + + + class Example(MDApp): + icons = list(md_icons.keys())[15:30] + + def build(self): + self.theme_cls.theme_style = "Dark" + self.theme_cls.primary_palette = "Orange" + return ( + MDBoxLayout( + MDTopAppBar(title="Example Tabs"), + MDTabs(id="tabs"), + orientation="vertical", + ) + ) + + def on_start(self): + self.root.ids.tabs.bind(on_tab_switch=self.on_tab_switch) + + for tab_name in self.icons: + self.root.ids.tabs.add_widget( + Tab( + MDIconButton( + icon=tab_name, + icon_size="48sp", + pos_hint={"center_x": .5, "center_y": .5}, + ), + icon=tab_name, + ) + ) + + def on_tab_switch( + self, instance_tabs, instance_tab, instance_tab_label, tab_text + ): + ''' + Called when switching tabs. + + :type instance_tabs: ; + :param instance_tab: <__main__.Tab object>; + :param instance_tab_label: ; + :param tab_text: text or name icon of tab; + ''' + + count_icon = instance_tab.icon # get the tab icon + print(f"Welcome to {count_icon}' tab'") + + + Example().run() + .. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/tabs-simple-example.gif :align: center @@ -138,62 +204,123 @@ Example with tab text if the tab has no icon, title or tab_label_text, the class will raise a ValueError. -.. code-block:: python +.. tabs:: - from kivy.lang import Builder + .. tab:: Declarative KV and imperative python styles - from kivymd.app import MDApp - from kivymd.uix.floatlayout import MDFloatLayout - from kivymd.uix.tab import MDTabsBase + .. code-block:: python - KV = ''' - MDBoxLayout: - orientation: "vertical" + from kivy.lang import Builder - MDTopAppBar: - title: "Example Tabs" + from kivymd.app import MDApp + from kivymd.uix.floatlayout import MDFloatLayout + from kivymd.uix.tab import MDTabsBase - MDTabs: - id: tabs - on_tab_switch: app.on_tab_switch(*args) + KV = ''' + MDBoxLayout: + orientation: "vertical" + + MDTopAppBar: + title: "Example Tabs" + + MDTabs: + id: tabs + on_tab_switch: app.on_tab_switch(*args) - + - MDLabel: - id: label - text: "Tab 0" - halign: "center" - ''' - - - class Tab(MDFloatLayout, MDTabsBase): - '''Class implementing content for a tab.''' - - - class Example(MDApp): - def build(self): - return Builder.load_string(KV) - - def on_start(self): - for i in range(20): - self.root.ids.tabs.add_widget(Tab(title=f"Tab {i}")) - - def on_tab_switch( - self, instance_tabs, instance_tab, instance_tab_label, tab_text - ): - '''Called when switching tabs. - - :type instance_tabs: ; - :param instance_tab: <__main__.Tab object>; - :param instance_tab_label: ; - :param tab_text: text or name icon of tab; + MDLabel: + id: label + text: "Tab 0" + halign: "center" ''' - instance_tab.ids.label.text = tab_text + + class Tab(MDFloatLayout, MDTabsBase): + '''Class implementing content for a tab.''' - Example().run() + class Example(MDApp): + def build(self): + self.theme_cls.theme_style = "Dark" + self.theme_cls.primary_palette = "Orange" + return Builder.load_string(KV) + + def on_start(self): + for i in range(20): + self.root.ids.tabs.add_widget(Tab(title=f"Tab {i}")) + + def on_tab_switch( + self, instance_tabs, instance_tab, instance_tab_label, tab_text + ): + '''Called when switching tabs. + + :type instance_tabs: ; + :param instance_tab: <__main__.Tab object>; + :param instance_tab_label: ; + :param tab_text: text or name icon of tab; + ''' + + instance_tab.ids.label.text = tab_text + + + Example().run() + + .. tab:: Declarative python style + + .. code-block:: python + + from kivymd.app import MDApp + from kivymd.uix.boxlayout import MDBoxLayout + from kivymd.uix.floatlayout import MDFloatLayout + from kivymd.uix.label import MDLabel + from kivymd.uix.tab import MDTabsBase, MDTabs + from kivymd.uix.toolbar import MDTopAppBar + + + class Tab(MDFloatLayout, MDTabsBase): + '''Class implementing content for a tab.''' + + + class Example(MDApp): + def build(self): + self.theme_cls.theme_style = "Dark" + self.theme_cls.primary_palette = "Orange" + return ( + MDBoxLayout( + MDTopAppBar(title="Example Tabs"), + MDTabs(id="tabs"), + orientation="vertical", + ) + ) + + def on_start(self): + self.root.ids.tabs.bind(on_tab_switch=self.on_tab_switch) + for i in range(20): + self.root.ids.tabs.add_widget( + Tab( + MDLabel(id="label", text="Tab 0", halign="center"), + title=f"Tab {i}", + ) + ) + + def on_tab_switch( + self, instance_tabs, instance_tab, instance_tab_label, tab_text + ): + ''' + Called when switching tabs. + + :type instance_tabs: ; + :param instance_tab: <__main__.Tab object>; + :param instance_tab_label: ; + :param tab_text: text or name icon of tab; + ''' + + instance_tab.ids.label.text = tab_text + + + Example().run() .. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/tabs-simple-example-text.gif :align: center @@ -201,119 +328,240 @@ Example with tab text Example with tab icon and text ------------------------------ -.. code-block:: python +.. tabs:: - from kivy.lang import Builder + .. tab:: Declarative KV and imperative python styles - from kivymd.app import MDApp - from kivymd.uix.tab import MDTabsBase - from kivymd.uix.floatlayout import MDFloatLayout - from kivymd.icon_definitions import md_icons + .. code-block:: python - KV = ''' - MDBoxLayout: - orientation: "vertical" + from kivy.lang import Builder - MDTopAppBar: - title: "Example Tabs" + from kivymd.app import MDApp + from kivymd.uix.tab import MDTabsBase + from kivymd.uix.floatlayout import MDFloatLayout + from kivymd.icon_definitions import md_icons - MDTabs: - id: tabs - ''' + KV = ''' + MDBoxLayout: + orientation: "vertical" + + MDTopAppBar: + title: "Example Tabs" + + MDTabs: + id: tabs + ''' - class Tab(MDFloatLayout, MDTabsBase): - pass + class Tab(MDFloatLayout, MDTabsBase): + pass - class Example(MDApp): - def build(self): - return Builder.load_string(KV) + class Example(MDApp): + def build(self): + self.theme_cls.theme_style = "Dark" + self.theme_cls.primary_palette = "Orange" + return Builder.load_string(KV) - def on_start(self): - for name_tab in list(md_icons.keys())[15:30]: - self.root.ids.tabs.add_widget(Tab(icon=name_tab, title=name_tab)) + def on_start(self): + for name_tab in list(md_icons.keys())[15:30]: + self.root.ids.tabs.add_widget(Tab(icon=name_tab, title=name_tab)) - Example().run() + Example().run() -.. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/tabs-simple-example-icon-text.png + .. tab:: Declarative python style + + .. code-block:: python + + from kivymd.app import MDApp + from kivymd.uix.boxlayout import MDBoxLayout + from kivymd.uix.tab import MDTabsBase, MDTabs + from kivymd.uix.floatlayout import MDFloatLayout + from kivymd.icon_definitions import md_icons + from kivymd.uix.toolbar import MDTopAppBar + + + class Tab(MDFloatLayout, MDTabsBase): + pass + + + class Example(MDApp): + def build(self): + self.theme_cls.theme_style = "Dark" + self.theme_cls.primary_palette = "Orange" + return ( + MDBoxLayout( + MDTopAppBar(title="Example Tabs"), + MDTabs(id="tabs"), + orientation="vertical", + ) + ) + + def on_start(self): + for name_tab in list(md_icons.keys())[15:30]: + self.root.ids.tabs.add_widget(Tab(icon=name_tab, title=name_tab)) + + + Example().run() + +.. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/tabs-simple-example-icon-text.gif :align: center Dynamic tab management ---------------------- -.. code-block:: python +.. tabs:: - from kivy.lang import Builder - from kivy.uix.scrollview import ScrollView + .. tab:: Declarative KV and imperative python styles - from kivymd.app import MDApp - from kivymd.uix.tab import MDTabsBase + .. code-block:: python - KV = ''' - MDBoxLayout: - orientation: "vertical" + from kivy.lang import Builder - MDTopAppBar: - title: "Example Tabs" - - MDTabs: - id: tabs - - - - - MDList: + from kivymd.uix.scrollview import MDScrollView + from kivymd.app import MDApp + from kivymd.uix.tab import MDTabsBase + KV = ''' MDBoxLayout: - adaptive_height: True + orientation: "vertical" - MDFlatButton: - text: "ADD TAB" - on_release: app.add_tab() + MDTopAppBar: + title: "Example Tabs" - MDFlatButton: - text: "REMOVE LAST TAB" - on_release: app.remove_tab() - - MDFlatButton: - text: "GET TAB LIST" - on_release: app.get_tab_list() - ''' + MDTabs: + id: tabs - class Tab(ScrollView, MDTabsBase): - '''Class implementing content for a tab.''' + + + MDList: + + MDBoxLayout: + adaptive_height: True + + MDFlatButton: + text: "ADD TAB" + on_release: app.add_tab() + + MDFlatButton: + text: "REMOVE LAST TAB" + on_release: app.remove_tab() + + MDFlatButton: + text: "GET TAB LIST" + on_release: app.get_tab_list() + ''' - class Example(MDApp): - index = 0 - - def build(self): - return Builder.load_string(KV) - - def on_start(self): - self.add_tab() - - def get_tab_list(self): - '''Prints a list of tab objects.''' - - print(self.root.ids.tabs.get_tab_list()) - - def add_tab(self): - self.index += 1 - self.root.ids.tabs.add_widget(Tab(text=f"{self.index} tab")) - - def remove_tab(self): - if self.index > 1: - self.index -= 1 - self.root.ids.tabs.remove_widget( - self.root.ids.tabs.get_tab_list()[-1] - ) + class Tab(MDScrollView, MDTabsBase): + '''Class implementing content for a tab.''' - Example().run() + class Example(MDApp): + index = 0 + + def build(self): + self.theme_cls.theme_style = "Dark" + self.theme_cls.primary_palette = "Orange" + return Builder.load_string(KV) + + def on_start(self): + self.add_tab() + + def get_tab_list(self): + '''Prints a list of tab objects.''' + + print(self.root.ids.tabs.get_tab_list()) + + def add_tab(self): + self.index += 1 + self.root.ids.tabs.add_widget(Tab(title=f"{self.index} tab")) + + def remove_tab(self): + if self.index > 1: + self.index -= 1 + self.root.ids.tabs.remove_widget( + self.root.ids.tabs.get_tab_list()[-1] + ) + + + Example().run() + + .. tab:: Declarative python style + + .. code-block:: python + + from kivymd.uix.button import MDFlatButton + from kivymd.uix.list import MDList + from kivymd.uix.scrollview import MDScrollView + from kivymd.app import MDApp + from kivymd.uix.boxlayout import MDBoxLayout + from kivymd.uix.tab import MDTabsBase, MDTabs + from kivymd.uix.toolbar import MDTopAppBar + + + class Tab(MDScrollView, MDTabsBase): + '''Class implementing content for a tab.''' + + + class Example(MDApp): + index = 0 + + def build(self): + self.theme_cls.theme_style = "Dark" + self.theme_cls.primary_palette = "Orange" + return ( + MDBoxLayout( + MDTopAppBar(title="Example Tabs"), + MDTabs(id="tabs"), + orientation="vertical", + ) + ) + + def on_start(self): + self.add_tab() + + def get_tab_list(self, *args): + '''Prints a list of tab objects.''' + + print(self.root.ids.tabs.get_tab_list()) + + def add_tab(self, *args): + self.index += 1 + self.root.ids.tabs.add_widget( + Tab( + MDList( + MDBoxLayout( + MDFlatButton( + text="ADD TAB", + on_release=self.add_tab, + ), + MDFlatButton( + text="REMOVE LAST TAB", + on_release=self.remove_tab, + ), + MDFlatButton( + text="GET TAB LIST", + on_release=self.get_tab_list, + ), + adaptive_height=True, + ), + ), + title=f"{self.index} tab", + ) + ) + + def remove_tab(self, *args): + if self.index > 1: + self.index -= 1 + self.root.ids.tabs.remove_widget( + self.root.ids.tabs.get_tab_list()[-1] + ) + + + Example().run() .. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/tabs-dynamic-managmant.gif :align: center @@ -324,83 +572,166 @@ Use on_ref_press method You can use markup for the text of the tabs and use the ``on_ref_press`` method accordingly: -.. code-block:: python +.. tabs:: - from kivy.lang import Builder + .. tab:: Declarative KV and imperative python styles - from kivymd.app import MDApp - from kivymd.uix.floatlayout import MDFloatLayout - from kivymd.font_definitions import fonts - from kivymd.uix.tab import MDTabsBase - from kivymd.icon_definitions import md_icons + .. code-block:: python - KV = ''' - MDBoxLayout: - orientation: "vertical" + from kivy.lang import Builder - MDTopAppBar: - title: "Example Tabs" + from kivymd.app import MDApp + from kivymd.uix.floatlayout import MDFloatLayout + from kivymd.font_definitions import fonts + from kivymd.uix.tab import MDTabsBase + from kivymd.icon_definitions import md_icons - MDTabs: - id: tabs - on_ref_press: app.on_ref_press(*args) + KV = ''' + MDBoxLayout: + orientation: "vertical" + + MDTopAppBar: + title: "Example Tabs" + + MDTabs: + id: tabs + on_ref_press: app.on_ref_press(*args) - + - MDIconButton: - id: icon - icon: app.icons[0] - user_font_size: "48sp" - pos_hint: {"center_x": .5, "center_y": .5} - ''' + MDIconButton: + id: icon + icon: app.icons[0] + icon_size: "48sp" + pos_hint: {"center_x": .5, "center_y": .5} + ''' - class Tab(MDFloatLayout, MDTabsBase): - '''Class implementing content for a tab.''' + class Tab(MDFloatLayout, MDTabsBase): + '''Class implementing content for a tab.''' - class Example(MDApp): - icons = list(md_icons.keys())[15:30] + class Example(MDApp): + icons = list(md_icons.keys())[15:30] - def build(self): - return Builder.load_string(KV) + def build(self): + self.theme_cls.theme_style = "Dark" + self.theme_cls.primary_palette = "Orange" + return Builder.load_string(KV) - def on_start(self): - for name_tab in self.icons: - self.root.ids.tabs.add_widget( - Tab( - text=f"[ref={name_tab}][font={fonts[-1]['fn_regular']}]{md_icons['close']}[/font][/ref] {name_tab}" + def on_start(self): + for name_tab in self.icons: + self.root.ids.tabs.add_widget( + Tab( + title=f"[ref={name_tab}][font={fonts[-1]['fn_regular']}]{md_icons['close']}[/font][/ref] {name_tab}" + ) + ) + + def on_ref_press( + self, + instance_tabs, + instance_tab_label, + instance_tab, + instance_tab_bar, + instance_carousel, + ): + ''' + The method will be called when the ``on_ref_press`` event + occurs when you, for example, use markup text for tabs. + + :param instance_tabs: + :param instance_tab_label: + :param instance_tab: <__main__.Tab object> + :param instance_tab_bar: + :param instance_carousel: + ''' + + # Removes a tab by clicking on the close icon on the left. + for instance_tab in instance_carousel.slides: + if instance_tab.title == instance_tab_label.text: + instance_tabs.remove_widget(instance_tab_label) + break + + + Example().run() + + .. tab:: Declarative python style + + .. code-block:: python + + from kivymd.app import MDApp + from kivymd.uix.boxlayout import MDBoxLayout + from kivymd.uix.button import MDIconButton + from kivymd.uix.floatlayout import MDFloatLayout + from kivymd.font_definitions import fonts + from kivymd.uix.tab import MDTabsBase, MDTabs + from kivymd.icon_definitions import md_icons + from kivymd.uix.toolbar import MDTopAppBar + + + class Tab(MDFloatLayout, MDTabsBase): + '''Class implementing content for a tab.''' + + + class Example(MDApp): + icons = list(md_icons.keys())[15:30] + + def build(self): + self.theme_cls.theme_style = "Dark" + self.theme_cls.primary_palette = "Orange" + return ( + MDBoxLayout( + MDTopAppBar(title="Example Tabs"), + MDTabs(id="tabs"), + orientation="vertical", + ) ) - ) - def on_ref_press( - self, - instance_tabs, - instance_tab_label, - instance_tab, - instance_tab_bar, - instance_carousel, - ): - ''' - The method will be called when the ``on_ref_press`` event - occurs when you, for example, use markup text for tabs. + def on_start(self): + self.root.ids.tabs.bind(on_ref_press=self.on_ref_press) + for name_tab in self.icons: + self.root.ids.tabs.add_widget( + Tab( + MDIconButton( + icon=self.icons[0], + icon_size="48sp", + pos_hint={"center_x": .5, "center_y": .5} + ), + title=( + f"[ref={name_tab}][font={fonts[-1]['fn_regular']}]" + f"{md_icons['close']}[/font][/ref] {name_tab}" + ), + ) + ) - :param instance_tabs: - :param instance_tab_label: - :param instance_tab: <__main__.Tab object> - :param instance_tab_bar: - :param instance_carousel: - ''' + def on_ref_press( + self, + instance_tabs, + instance_tab_label, + instance_tab, + instance_tab_bar, + instance_carousel, + ): + ''' + The method will be called when the ``on_ref_press`` event + occurs when you, for example, use markup text for tabs. - # Removes a tab by clicking on the close icon on the left. - for instance_tab in instance_carousel.slides: - if instance_tab.text == instance_tab_label.text: - instance_tabs.remove_widget(instance_tab_label) - break + :param instance_tabs: + :param instance_tab_label: + :param instance_tab: <__main__.Tab object> + :param instance_tab_bar: + :param instance_carousel: + ''' + + # Removes a tab by clicking on the close icon on the left. + for instance_tab in instance_carousel.slides: + if instance_tab.title == instance_tab_label.text: + instance_tabs.remove_widget(instance_tab_label) + break - Example().run() + Example().run() .. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/tabs-on-ref-press.gif :align: center @@ -408,88 +739,182 @@ method accordingly: Switching the tab by name ------------------------- -.. code-block:: python +.. tabs:: - from kivy.lang import Builder + .. tab:: Declarative KV and imperative python styles - from kivymd.app import MDApp - from kivymd.icon_definitions import md_icons - from kivymd.uix.floatlayout import MDFloatLayout - from kivymd.uix.tab import MDTabsBase + .. code-block:: python - KV = ''' - MDBoxLayout: - orientation: "vertical" + from kivy.lang import Builder - MDTopAppBar: - title: "Example Tabs" + from kivymd.app import MDApp + from kivymd.icon_definitions import md_icons + from kivymd.uix.floatlayout import MDFloatLayout + from kivymd.uix.tab import MDTabsBase - MDTabs: - id: tabs + KV = ''' + MDBoxLayout: + orientation: "vertical" + + MDTopAppBar: + title: "Example Tabs" + + MDTabs: + id: tabs - + - MDBoxLayout: - orientation: "vertical" - pos_hint: {"center_x": .5, "center_y": .5} - size_hint: None, None - spacing: dp(48) + MDBoxLayout: + orientation: "vertical" + pos_hint: {"center_x": .5, "center_y": .5} + adaptive_size: True + spacing: dp(48) - MDIconButton: - id: icon - icon: "arrow-right" - user_font_size: "48sp" - on_release: app.switch_tab_by_name() + MDIconButton: + id: icon + icon: "arrow-right" + icon_size: "48sp" + on_release: app.switch_tab_by_name() - MDIconButton: - id: icon2 - icon: "page-next" - user_font_size: "48sp" - on_release: app.switch_tab_by_object() - ''' + MDIconButton: + id: icon2 + icon: "page-next" + icon_size: "48sp" + on_release: app.switch_tab_by_object() + ''' - class Tab(MDFloatLayout, MDTabsBase): - '''Class implementing content for a tab.''' + class Tab(MDFloatLayout, MDTabsBase): + '''Class implementing content for a tab.''' - class Example(MDApp): - icons = list(md_icons.keys())[15:30] + class Example(MDApp): + icons = list(md_icons.keys())[15:30] - def build(self): - self.iter_list_names = iter(list(self.icons)) - return Builder.load_string(KV) + def build(self): + self.theme_cls.theme_style = "Dark" + self.theme_cls.primary_palette = "Orange" + self.iter_list_names = iter(list(self.icons)) + return Builder.load_string(KV) - def on_start(self): - for name_tab in list(self.icons): - self.root.ids.tabs.add_widget(Tab(tab_label_text=name_tab)) - self.iter_list_objects = iter(list(self.root.ids.tabs.get_tab_list())) + def on_start(self): + for name_tab in list(self.icons): + self.root.ids.tabs.add_widget(Tab(tab_label_text=name_tab)) + self.iter_list_objects = iter(list(self.root.ids.tabs.get_tab_list())) - def switch_tab_by_object(self): - try: - x = next(self.iter_list_objects) - print(f"Switch slide by object, next element to show: [{x}]") - self.root.ids.tabs.switch_tab(x) - except StopIteration: - # reset the iterator an begin again. - self.iter_list_objects = iter(list(self.root.ids.tabs.get_tab_list())) - self.switch_tab_by_object() + def switch_tab_by_object(self): + try: + x = next(self.iter_list_objects) + print(f"Switch slide by object, next element to show: [{x}]") + self.root.ids.tabs.switch_tab(x) + except StopIteration: + # reset the iterator an begin again. + self.iter_list_objects = iter(list(self.root.ids.tabs.get_tab_list())) + self.switch_tab_by_object() - def switch_tab_by_name(self): - '''Switching the tab by name.''' + def switch_tab_by_name(self): + '''Switching the tab by name.''' - try: - x = next(self.iter_list_names) - print(f"Switch slide by name, next element to show: [{x}]") - self.root.ids.tabs.switch_tab(x) - except StopIteration: - # Reset the iterator an begin again. - self.iter_list_names = iter(list(self.icons)) - self.switch_tab_by_name() + try: + x = next(self.iter_list_names) + print(f"Switch slide by name, next element to show: [{x}]") + self.root.ids.tabs.switch_tab(x) + except StopIteration: + # Reset the iterator an begin again. + self.iter_list_names = iter(list(self.icons)) + self.switch_tab_by_name() - Example().run() + Example().run() + + .. tab:: Declarative python style + + .. code-block:: python + + from kivy.metrics import dp + + from kivymd.app import MDApp + from kivymd.icon_definitions import md_icons + from kivymd.uix.boxlayout import MDBoxLayout + from kivymd.uix.button import MDIconButton + from kivymd.uix.floatlayout import MDFloatLayout + from kivymd.uix.tab import MDTabsBase, MDTabs + from kivymd.uix.toolbar import MDTopAppBar + + + class Tab(MDFloatLayout, MDTabsBase): + '''Class implementing content for a tab.''' + + + class Example(MDApp): + icons = list(md_icons.keys())[15:30] + + def build(self): + self.theme_cls.theme_style = "Dark" + self.theme_cls.primary_palette = "Orange" + self.iter_list_names = iter(list(self.icons)) + return ( + MDBoxLayout( + MDTopAppBar(title="Example Tabs"), + MDTabs(id="tabs"), + orientation="vertical", + ) + ) + + def on_start(self): + for name_tab in list(self.icons): + self.root.ids.tabs.add_widget( + Tab( + MDBoxLayout( + MDIconButton( + id="icon", + icon="arrow-right", + icon_size="48sp", + on_release=self.switch_tab_by_name, + ), + MDIconButton( + id="icon2", + icon="arrow-left", + icon_size="48sp", + on_release=self.switch_tab_by_object, + ), + orientation="vertical", + pos_hint={"center_x": .5, "center_y": .5}, + adaptive_size=True, + spacing=dp(48), + ), + tab_label_text=name_tab, + ) + ) + + self.iter_list_objects = iter(list(self.root.ids.tabs.get_tab_list())) + + def switch_tab_by_object(self, *args): + try: + x = next(self.iter_list_objects) + print(f"Switch slide by object, next element to show: [{x}]") + self.root.ids.tabs.switch_tab(x) + except StopIteration: + # reset the iterator an begin again. + self.iter_list_objects = iter( + list(self.root.ids.tabs.get_tab_list())) + self.switch_tab_by_object() + + def switch_tab_by_name(self, *args): + '''Switching the tab by name.''' + + try: + x = next(self.iter_list_names) + print(f"Switch slide by name, next element to show: [{x}]") + self.root.ids.tabs.switch_tab(x) + except StopIteration: + # Reset the iterator an begin again. + self.iter_list_names = iter(list(self.icons)) + self.switch_tab_by_name() + + + Example().run() .. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/switching-tab-by-name.gif :align: center @@ -519,7 +944,6 @@ from kivy.properties import ( from kivy.uix.anchorlayout import AnchorLayout from kivy.uix.behaviors import ToggleButtonBehavior from kivy.uix.scrollview import ScrollView -from kivy.uix.widget import Widget from kivy.utils import boundary from kivymd import uix_path @@ -527,11 +951,12 @@ from kivymd.font_definitions import fonts, theme_font_styles from kivymd.icon_definitions import md_icons from kivymd.theming import ThemableBehavior, ThemeManager from kivymd.uix.behaviors import ( - FakeRectangularElevationBehavior, + DeclarativeBehavior, RectangularRippleBehavior, SpecificBackgroundColorBehavior, ) from kivymd.uix.boxlayout import MDBoxLayout +from kivymd.uix.card import MDCard from kivymd.uix.carousel import MDCarousel from kivymd.uix.label import MDLabel @@ -564,10 +989,15 @@ class MDTabsLabel(ToggleButtonBehavior, RectangularRippleBehavior, MDLabel): ) def on_release(self) -> None: - self.tab_bar.parent.dispatch("on_tab_switch", self.tab, self, self.text) - # If the label is selected load the relative tab from carousel. - if self.state == "down": - self.tab_bar.parent.carousel.load_slide(self.tab) + try: + self.tab_bar.parent.dispatch( + "on_tab_switch", self.tab, self, self.text + ) + # If the label is selected load the relative tab from carousel. + if self.state == "down": + self.tab_bar.parent.carousel.load_slide(self.tab) + except KeyError: + pass def on_texture(self, instance_tabs_label, texture: Texture) -> None: # Just save the minimum width of the label based of the content. @@ -593,7 +1023,7 @@ class MDTabsLabel(ToggleButtonBehavior, RectangularRippleBehavior, MDLabel): Clock.schedule_once(self.tab_bar._label_request_indicator_update, 0) -class MDTabsBase(Widget): +class MDTabsBase: """ This class allow you to create a tab. You must create a new class that inherits from MDTabsBase. @@ -645,22 +1075,6 @@ class MDTabsBase(Widget): and defaults to `True`. """ - text = StringProperty(deprecated=True) - """ - This property is the actual title of the tab. - use the property :attr:`icon` and :attr:`title` to set this property - correctly. - - This property is kept public for specific and backward compatibility - purposes. - - :attr:`text` is an :class:`~kivy.properties.StringProperty` - and defaults to `''`. - - .. deprecated:: 1.0.0 - Use :attr:`tab_label_text` instead. - """ - tab_label_text = StringProperty() """ This property is the actual title's Label of the tab. @@ -715,14 +1129,13 @@ class MDTabsBase(Widget): This property will affect the Tab's Title Label widget. """ - def __init__(self, **kwargs): + def __init__(self, *args, **kwargs): self.tab_label = MDTabsLabel(tab=self) - super().__init__(**kwargs) + super().__init__(*args, **kwargs) self.bind( icon=self._update_text, title=self._update_text, title_icon_mode=self._update_text, - text=self.update_label_text, tab_label_text=self.update_label_text, title_is_capital=self.update_label_text, ) @@ -742,7 +1155,7 @@ class MDTabsBase(Widget): self.tab_label_text = f"[size=24sp][font={fonts[-1]['fn_regular']}]{md_icons[self.icon]}[/size][/font]" if self.title: self.tab_label_text = ( - self.text + self.tab_label_text + (" " if self.title_icon_mode == "Lead" else "\n") + self.title ) @@ -769,10 +1182,7 @@ class MDTabsBase(Widget): self.update_label_text(None, self.tab_label_text) def update_label_text(self, instance_user_tab, text_tab: str) -> None: - self.tab_label.text = self.text = self.tab_label_text - - def on_text(self, instance_user_tab, text_tab: str) -> None: - self.tab_label_text = self.text + self.tab_label.text = text_tab class MDTabsMain(MDBoxLayout): @@ -862,9 +1272,7 @@ class MDTabsScrollView(ScrollView): _update(self.effect_y, scroll_y) -class MDTabsBar( - ThemableBehavior, FakeRectangularElevationBehavior, MDBoxLayout -): +class MDTabsBar(MDCard): """ This class is just a boxlayout that contains the scroll view for tabs. It is also responsible for resizing the tab shortcut when necessary. @@ -997,7 +1405,12 @@ class MDTabsBar( self.update_indicator(widget.x, widget.width) -class MDTabs(ThemableBehavior, SpecificBackgroundColorBehavior, AnchorLayout): +class MDTabs( + DeclarativeBehavior, + ThemableBehavior, + SpecificBackgroundColorBehavior, + AnchorLayout, +): """ You can use this class to create your own tabbed panel. @@ -1135,13 +1548,43 @@ class MDTabs(ThemableBehavior, SpecificBackgroundColorBehavior, AnchorLayout): and defaults to `None`. """ + shadow_softness = NumericProperty(12) + """ + See :attr:`kivymd.uix.behaviors.CommonElevationBehavior.shadow_softness` + attribute. + + .. versionadded:: 1.1.0 + + :attr:`shadow_softness` is an :class:`~kivy.properties.NumericProperty` + and defaults to `12`. + """ + + shadow_color = ColorProperty([0, 0, 0, 0.6]) + """ + See :attr:`kivymd.uix.behaviors.CommonElevationBehavior.shadow_color` + attribute. + + .. versionadded:: 1.1.0 + + :attr:`shadow_color` is an :class:`~kivy.properties.ColorProperty` + and defaults to `[0, 0, 0, 0.6]`. + """ + + shadow_offset = ListProperty((0, 0)) + """ + See :attr:`kivymd.uix.behaviors.CommonElevationBehavior.shadow_offset` + attribute. + + .. versionadded:: 1.1.0 + + :attr:`shadow_offset` is an :class:`~kivy.properties.ListProperty` + and defaults to `[0, 0]`. + """ + elevation = NumericProperty(0) """ - Tab value elevation. - - .. seealso:: - - `Behaviors/Elevation `_ + See :attr:`kivymd.uix.behaviors.CommonElevationBehavior.elevation` + attribute. :attr:`elevation` is an :class:`~kivy.properties.NumericProperty` and defaults to `0`. @@ -1204,8 +1647,8 @@ class MDTabs(ThemableBehavior, SpecificBackgroundColorBehavior, AnchorLayout): and defaults to `True`. """ - def __init__(self, **kwargs): - super().__init__(**kwargs) + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) self.register_event_type("on_tab_switch") self.register_event_type("on_ref_press") self.register_event_type("on_slide_progress") diff --git a/sbapp/kivymd/uix/taptargetview.py b/sbapp/kivymd/uix/taptargetview.py index 1e57ff0..735de0d 100644 --- a/sbapp/kivymd/uix/taptargetview.py +++ b/sbapp/kivymd/uix/taptargetview.py @@ -487,6 +487,8 @@ class MDTapTargetView(ThemableBehavior, EventDispatcher): _outer_radius = NumericProperty(0) _target_radius = NumericProperty(0) + __elevation = 0 + def __init__(self, **kwargs): self.ripple_max_dist = dp(90) self.on_outer_radius(self, self.outer_radius) @@ -514,6 +516,89 @@ class MDTapTargetView(ThemableBehavior, EventDispatcher): if not self.outer_circle_color: self.outer_circle_color = self.theme_cls.primary_color[:-1] + def start(self, *args): + """Starts widget opening animation.""" + + self._initialize() + self._animate_outer() + self.state = "open" + self.core_title_text.opacity = 1 + self.core_description_text.opacity = 1 + self.dispatch("on_open") + + elevation = getattr(self.widget, "elevation", None) + if elevation: + self.__elevation = elevation + self.widget.elevation = 0 + + def stop(self, *args): + """Starts widget close animation.""" + + # It needs a better implementation. + if self.anim_ripple is not None: + self.anim_ripple.unbind(on_complete=self._repeat_ripple) + self.core_title_text.opacity = 0 + self.core_description_text.opacity = 0 + anim = Animation( + d=0.15, + t="in_cubic", + **dict( + zip( + ["_outer_radius", "_target_radius", "target_ripple_radius"], + [0, 0, 0], + ) + ), + ) + anim.bind(on_complete=self._after_stop) + anim.start(self.widget) + + def on_open(self, *args): + """Called at the time of the start of the widget opening animation.""" + + def on_close(self, *args): + """Called at the time of the start of the widget closed animation.""" + + def on_draw_shadow(self, instance, value): + Logger.warning( + "The shadow adding method will be implemented in future versions" + ) + + def on_description_text(self, instance, value): + self.core_description_text.text = value + + def on_description_text_size(self, instance, value): + self.core_description_text.font_size = value + + def on_description_text_bold(self, instance, value): + self.core_description_text.bold = value + + def on_title_text(self, instance, value): + self.core_title_text.text = value + + def on_title_text_size(self, instance, value): + self.core_title_text.font_size = value + + def on_title_text_bold(self, instance, value): + self.core_title_text.bold = value + + def on_outer_radius(self, instance, value): + self._outer_radius = self.outer_radius * 2 + + def on_target_radius(self, instance, value): + self._target_radius = self.target_radius * 2 + + def on_target_touch(self): + if self.stop_on_target_touch: + self.stop() + + def on_outer_touch(self): + if self.stop_on_outer_touch: + self.stop() + + def on_outside_click(self): + if self.cancelable: + self.stop() + def _initialize(self): setattr(self.widget, "_outer_radius", 0) setattr(self.widget, "_target_radius", 0) @@ -527,7 +612,7 @@ class MDTapTargetView(ThemableBehavior, EventDispatcher): def _draw_canvas(self): _pos = self._ttv_pos() - self.widget.canvas.before.clear() + self.widget.canvas.before.remove_group("ttv_group") with self.widget.canvas.before: # Outer circle. @@ -588,34 +673,14 @@ class MDTapTargetView(ThemableBehavior, EventDispatcher): group="ttv_group", ) - def stop(self, *args): - """Starts widget close animation.""" - - # It needs a better implementation. - if self.anim_ripple is not None: - self.anim_ripple.unbind(on_complete=self._repeat_ripple) - self.core_title_text.opacity = 0 - self.core_description_text.opacity = 0 - anim = Animation( - d=0.15, - t="in_cubic", - **dict( - zip( - ["_outer_radius", "_target_radius", "target_ripple_radius"], - [0, 0, 0], - ) - ), - ) - anim.bind(on_complete=self._after_stop) - anim.start(self.widget) - def _after_stop(self, *args): self.widget.canvas.before.remove_group("ttv_group") args[0].stop_all(self.widget) - elev = getattr(self.widget, "elevation", None) - if elev: - self._fix_elev() + elevation = getattr(self.widget, "elevation", None) + if elevation: + self.widget.elevation = self.__elevation + self.dispatch("on_close") # Don't forget to unbind the function or it'll mess @@ -639,16 +704,6 @@ class MDTapTargetView(ThemableBehavior, EventDispatcher): ) Color(a=1) - def start(self, *args): - """Starts widget opening animation.""" - - self._initialize() - self._animate_outer() - self.state = "open" - self.core_title_text.opacity = 1 - self.core_description_text.opacity = 1 - self.dispatch("on_open") - def _animate_outer(self): anim = Animation( d=0.2, @@ -684,53 +739,6 @@ class MDTapTargetView(ThemableBehavior, EventDispatcher): setattr(self.widget, "target_ripple_alpha", 1) self._animate_ripple() - def on_open(self, *args): - """Called at the time of the start of the widget opening animation.""" - - def on_close(self, *args): - """Called at the time of the start of the widget closed animation.""" - - def on_draw_shadow(self, instance, value): - Logger.warning( - "The shadow adding method will be implemented in future versions" - ) - - def on_description_text(self, instance, value): - self.core_description_text.text = value - - def on_description_text_size(self, instance, value): - self.core_description_text.font_size = value - - def on_description_text_bold(self, instance, value): - self.core_description_text.bold = value - - def on_title_text(self, instance, value): - self.core_title_text.text = value - - def on_title_text_size(self, instance, value): - self.core_title_text.font_size = value - - def on_title_text_bold(self, instance, value): - self.core_title_text.bold = value - - def on_outer_radius(self, instance, value): - self._outer_radius = self.outer_radius * 2 - - def on_target_radius(self, instance, value): - self._target_radius = self.target_radius * 2 - - def on_target_touch(self): - if self.stop_on_target_touch: - self.stop() - - def on_outer_touch(self): - if self.stop_on_outer_touch: - self.stop() - - def on_outside_click(self): - if self.cancelable: - self.stop() - def _some_func(self, wid, touch): """ This function decides which one to dispatch based on the touch diff --git a/sbapp/kivymd/uix/templates/rotatewidget/rotatewidget.kv b/sbapp/kivymd/uix/templates/rotatewidget/rotatewidget.kv deleted file mode 100644 index 77b57df..0000000 --- a/sbapp/kivymd/uix/templates/rotatewidget/rotatewidget.kv +++ /dev/null @@ -1,9 +0,0 @@ - - canvas.before: - PushMatrix - Rotate: - angle: self.rotate_value_angle - axis: tuple(self.rotate_value_axis) - origin: self.center - canvas.after: - PopMatrix diff --git a/sbapp/kivymd/uix/templates/rotatewidget/rotatewidget.py b/sbapp/kivymd/uix/templates/rotatewidget/rotatewidget.py index d3491df..9416a5f 100644 --- a/sbapp/kivymd/uix/templates/rotatewidget/rotatewidget.py +++ b/sbapp/kivymd/uix/templates/rotatewidget/rotatewidget.py @@ -2,127 +2,31 @@ Templates/RotateWidget ====================== -.. versionadded:: 1.0.0 +.. deprecated:: 1.0.0 -Base class for controlling the rotate of the widget. - -.. note:: See `kivy.graphics.Rotate - `_ - for more information. - -Kivy ----- - -.. code-block:: python - - from kivy.animation import Animation - from kivy.lang import Builder - from kivy.app import App - from kivy.properties import NumericProperty - from kivy.uix.button import Button - - KV = ''' - Screen: - - RotateButton: - size_hint: .5, .5 - pos_hint: {"center_x": .5, "center_y": .5} - on_release: app.change_rotate(self) - - canvas.before: - PushMatrix - Rotate: - angle: self.rotate_value_angle - axis: 0, 0, 1 - origin: self.center - canvas.after: - PopMatrix - ''' - - - class RotateButton(Button): - rotate_value_angle = NumericProperty(0) - - - class Test(App): - def build(self): - return Builder.load_string(KV) - - def change_rotate(self, instance_button: Button) -> None: - Animation(rotate_value_angle=45, d=0.3).start(instance_button) - - - Test().run() - -KivyMD ------- - -.. code-block:: python - - from kivy.animation import Animation - from kivy.lang import Builder - - from kivymd.app import MDApp - from kivymd.uix.button import MDRaisedButton - from kivymd.uix.templates import RotateWidget - - KV = ''' - MDScreen: - - RotateButton: - size_hint: .5, .5 - pos_hint: {"center_x": .5, "center_y": .5} - on_release: app.change_rotate(self) - elevation:0 - ''' - - - class RotateButton(MDRaisedButton, RotateWidget): - pass - - - class Test(MDApp): - def build(self): - return Builder.load_string(KV) - - def change_rotate(self, instance_button: MDRaisedButton) -> None: - Animation(rotate_value_angle=45, d=0.3).start(instance_button) - - - Test().run() +.. note:: `RotateWidget` class has been deprecated. Please use + `RotateBahavior `_ + class instead. """ __all__ = ("RotateWidget",) -import os +from kivy import Logger -from kivy.lang import Builder -from kivy.properties import ListProperty, NumericProperty - -from kivymd import uix_path - -with open( - os.path.join(uix_path, "templates", "rotatewidget", "rotatewidget.kv"), - encoding="utf-8", -) as kv_file: - Builder.load_string(kv_file.read()) +from kivymd.uix.behaviors import RotateBehavior -class RotateWidget: - """Base class for controlling the rotate of the widget.""" - - rotate_value_angle = NumericProperty(0) +class RotateWidget(RotateBehavior): """ - Property for getting/setting the angle of the rotation. - - :attr:`rotate_value_angle` is an :class:`~kivy.properties.NumericProperty` - and defaults to `0`. + .. deprecated:: 1.1.0 + Use :class:`~kivymd.uix.behaviors.rotate_behavior.RotateBehavior` + class instead. """ - rotate_value_axis = ListProperty((0, 0, 1)) - """ - Property for getting/setting the axis of the rotation. - - :attr:`rotate_value_axis` is an :class:`~kivy.properties.NumericProperty` - and defaults to `(0, 0, 1)`. - """ + def __init__(self, **kwargs): + super().__init__(**kwargs) + Logger.warning( + "KivyMD: " + "The `RotateWidget` class has been deprecated. " + "Use the `RotateBehavior` class instead." + ) diff --git a/sbapp/kivymd/uix/templates/scalewidget/scalewidget.kv b/sbapp/kivymd/uix/templates/scalewidget/scalewidget.kv deleted file mode 100644 index 5fe002f..0000000 --- a/sbapp/kivymd/uix/templates/scalewidget/scalewidget.kv +++ /dev/null @@ -1,10 +0,0 @@ - - canvas.before: - PushMatrix - Scale: - x: self.scale_value_x - y: self.scale_value_y - z: self.scale_value_x - origin: self.center - canvas.after: - PopMatrix diff --git a/sbapp/kivymd/uix/templates/scalewidget/scalewidget.py b/sbapp/kivymd/uix/templates/scalewidget/scalewidget.py index 9f9204f..3fe6985 100644 --- a/sbapp/kivymd/uix/templates/scalewidget/scalewidget.py +++ b/sbapp/kivymd/uix/templates/scalewidget/scalewidget.py @@ -2,149 +2,33 @@ Templates/ScaleWidget ===================== -.. versionadded:: 1.0.0 +.. deprecated:: 1.1.0 Base class for controlling the scale of the widget. -.. note:: See `kivy.graphics.Scale - `_ - for more information. - -Kivy ----- - -.. code-block:: python - - from kivy.animation import Animation - from kivy.lang import Builder - from kivy.properties import NumericProperty - from kivy.uix.button import Button - from kivy.app import App - - - KV = ''' - Screen: - - ScaleButton: - size_hint: .5, .5 - pos_hint: {"center_x": .5, "center_y": .5} - on_release: app.change_scale(self) - - canvas.before: - PushMatrix - Scale: - x: self.scale_value_x - y: self.scale_value_y - z: self.scale_value_x - origin: self.center - canvas.after: - PopMatrix - ''' - - - class ScaleButton(Button): - scale_value_x = NumericProperty(1) - scale_value_y = NumericProperty(1) - scale_value_z = NumericProperty(1) - - - class Test(App): - def build(self): - return Builder.load_string(KV) - - def change_scale(self, instance_button: Button) -> None: - Animation( - scale_value_x=0.5, - scale_value_y=0.5, - scale_value_z=0.5, - d=0.3, - ).start(instance_button) - - - Test().run() - -KivyMD ------- - -.. code-block:: python - - from kivy.animation import Animation - from kivy.lang import Builder - - from kivymd.app import MDApp - from kivymd.uix.button import MDRaisedButton - from kivymd.uix.templates import ScaleWidget - - KV = ''' - MDScreen: - - ScaleButton: - size_hint: .5, .5 - pos_hint: {"center_x": .5, "center_y": .5} - on_release: app.change_scale(self) - elevation:0 - ''' - - - class ScaleButton(MDRaisedButton, ScaleWidget): - pass - - - class Test(MDApp): - def build(self): - return Builder.load_string(KV) - - def change_scale(self, instance_button: MDRaisedButton) -> None: - Animation( - scale_value_x=0.5, - scale_value_y=0.5, - scale_value_z=0.5, - d=0.3, - ).start(instance_button) - - - Test().run() +.. note:: `ScaleWidget` class has been deprecated. Please use + `ScaleBehavior `_ + class instead. """ __all__ = ("ScaleWidget",) -import os +from kivy import Logger -from kivy.lang import Builder -from kivy.properties import NumericProperty - -from kivymd import uix_path - -with open( - os.path.join(uix_path, "templates", "scalewidget", "scalewidget.kv"), - encoding="utf-8", -) as kv_file: - Builder.load_string(kv_file.read()) +from kivymd.uix.behaviors import ScaleBehavior -class ScaleWidget: - """Base class for controlling the scale of the widget.""" - - scale_value_x = NumericProperty(1) +class ScaleWidget(ScaleBehavior): """ - X-axis value. - - :attr:`scale_value_x` is an :class:`~kivy.properties.NumericProperty` - and defaults to `1`. + .. deprecated:: 1.1.0 + Use :class:`~kivymd.uix.behaviors.scale_behavior.ScaleBehavior` + class instead. """ - scale_value_y = NumericProperty(1) - """ - Y-axis value. - - :attr:`scale_value_y` is an :class:`~kivy.properties.NumericProperty` - and defaults to `1`. - """ - - scale_value_z = NumericProperty(1) - """ - Z-axis value. - - :attr:`scale_value_z` is an :class:`~kivy.properties.NumericProperty` - and defaults to `1`. - """ + def __init__(self, **kwargs): + super().__init__(**kwargs) + Logger.warning( + "KivyMD: " + "The `ScaleWidget` class has been deprecated. " + "Use the `ScaleBehavior` class instead." + ) diff --git a/sbapp/kivymd/uix/templates/stencilwidget/stencilwidget.kv b/sbapp/kivymd/uix/templates/stencilwidget/stencilwidget.kv deleted file mode 100644 index 04807dc..0000000 --- a/sbapp/kivymd/uix/templates/stencilwidget/stencilwidget.kv +++ /dev/null @@ -1,19 +0,0 @@ - - canvas.before: - StencilPush - RoundedRectangle: - pos: root.pos - size: root.size - # FIXME: Sometimes the radius has the value [], which get a - # `GraphicException: Invalid radius value, must be list of tuples/numerics` error - radius: root.radius if root.radius else [0, 0, 0, 0] - StencilUse - canvas.after: - StencilUnUse - RoundedRectangle: - pos: root.pos - size: root.size - # FIXME: Sometimes the radius has the value [], which get a - # `GraphicException: Invalid radius value, must be list of tuples/numerics` error - radius: root.radius if root.radius else [0, 0, 0, 0] - StencilPop diff --git a/sbapp/kivymd/uix/templates/stencilwidget/stencilwidget.py b/sbapp/kivymd/uix/templates/stencilwidget/stencilwidget.py index 3aa8716..a231588 100644 --- a/sbapp/kivymd/uix/templates/stencilwidget/stencilwidget.py +++ b/sbapp/kivymd/uix/templates/stencilwidget/stencilwidget.py @@ -2,115 +2,33 @@ Templates/StencilWidget ======================= -.. versionadded:: 1.0.0 +.. deprecated:: 1.1.0 Base class for controlling the stencil instructions of the widget. -.. note:: See `Stencil instructions - `_ - for more information. - -Kivy ----- - -.. code-block:: python - - from kivy.lang import Builder - from kivy.app import App - - KV = ''' - Carousel: - - Button: - size_hint: .9, .8 - pos_hint: {"center_x": .5, "center_y": .5} - - canvas.before: - StencilPush - RoundedRectangle: - pos: root.pos - size: root.size - StencilUse - canvas.after: - StencilUnUse - RoundedRectangle: - pos: root.pos - size: root.size - StencilPop - ''' - - - class Test(App): - def build(self): - return Builder.load_string(KV) - - - Test().run() - -KivyMD ------- - -.. code-block:: python - - from kivy.lang import Builder - - from kivymd.app import MDApp - from kivymd.uix.templates import StencilWidget - from kivymd.utils.fitimage import FitImage - - KV = ''' - MDCarousel: - - StencilImage: - size_hint: .9, .8 - pos_hint: {"center_x": .5, "center_y": .5} - source: "image.png" - ''' - - - class StencilImage(FitImage, StencilWidget): - pass - - - class Test(MDApp): - def build(self): - return Builder.load_string(KV) - - - Test().run() +.. note:: `StencilWidget` class has been deprecated. Please use + `StencilBehavior `_ + class instead. """ __all__ = ("StencilWidget",) -import os +from kivy import Logger -from kivy.lang import Builder -from kivy.properties import VariableListProperty - -from kivymd import uix_path - -with open( - os.path.join(uix_path, "templates", "stencilwidget", "stencilwidget.kv"), - encoding="utf-8", -) as kv_file: - Builder.load_string(kv_file.read()) +from kivymd.uix.behaviors import StencilBehavior -class StencilWidget: - """Base class for controlling the stencil instructions of the widget""" - - radius = VariableListProperty([0], length=4) +class StencilWidget(StencilBehavior): """ - Canvas radius. - - .. versionadded:: 1.0.0 - - .. code-block:: python - - # Top left corner slice. - MDWidget: - radius: [25, 0, 0, 0] - - :attr:`radius` is an :class:`~kivy.properties.VariableListProperty` - and defaults to `[0, 0, 0, 0]`. + .. deprecated:: 1.1.0 + Use :class:`~kivymd.uix.behaviors.scale_behavior.StencilBehavior` + class instead. """ + + def __init__(self, **kwargs): + super().__init__(**kwargs) + Logger.warning( + "KivyMD: " + "The `StencilWidget` class has been deprecated. " + "Use the `StencilBehavior` class instead." + ) diff --git a/sbapp/kivymd/uix/textfield/__init__.py b/sbapp/kivymd/uix/textfield/__init__.py index 6ac1738..d3786dd 100644 --- a/sbapp/kivymd/uix/textfield/__init__.py +++ b/sbapp/kivymd/uix/textfield/__init__.py @@ -1,2 +1,2 @@ # NOQA F401 -from .textfield import MDTextField, MDTextFieldRect, MDTextFieldRound +from .textfield import MDTextField, MDTextFieldRect diff --git a/sbapp/kivymd/uix/textfield/textfield.kv b/sbapp/kivymd/uix/textfield/textfield.kv index 3f73c32..6f11abb 100644 --- a/sbapp/kivymd/uix/textfield/textfield.kv +++ b/sbapp/kivymd/uix/textfield/textfield.kv @@ -1,7 +1,7 @@ -#:import images_path kivymd.images_path - - + input_filter: self.field_filter + do_backspace: self.do_backspace + canvas.before: Clear @@ -11,16 +11,16 @@ Ellipse: angle_start: 180 angle_end: 360 - pos: self.pos[0] - self.size[1] / 2, self.pos[1] - size: self.size[1], self.size[1] + pos: self.x - self.height / 2 + dp(18), self.y + size: self.height, self.height Ellipse: angle_start: 360 angle_end: 540 - pos: self.size[0] + self.pos[0] - self.size[1]/2.0, self.pos[1] - size: self.size[1], self.size[1] + pos: (self.width - dp(18)) + self.x - self.height / 2.0, self.y + size: self.height, self.height Rectangle: - pos: self.pos - size: self.size + pos: self.x + dp(9), self.y + size: self.width - dp(18), self.height Color: rgba: @@ -33,30 +33,30 @@ (0, 0, 0, 0) Line: points: - self.pos[0], \ - self.pos[1], \ - self.pos[0] + self.size[0], \ - self.pos[1] + self.x + dp(18), \ + self.y, \ + self.x + self.width - dp(18), \ + self.y Line: points: - self.pos[0], \ - self.pos[1] + self.size[1], \ - self.pos[0] + self.size[0], \ - self.pos[1] + self.size[1] + self.x + dp(18), \ + self.y + self.height, \ + self.x + self.width - dp(18), \ + self.y + self.height Line: ellipse: - self.pos[0] - self.size[1] / 2, \ - self.pos[1], \ - self.size[1], \ - self.size[1], \ + self.x - self.height / 2 + dp(18), \ + self.y, \ + self.height, \ + self.height, \ 180, \ 360 Line: ellipse: - self.size[0] + self.pos[0] - self.size[1] / 2.0, \ - self.pos[1], \ - self.size[1], \ - self.size[1], \ + self.width + self.x - self.height / 2.0 - dp(18), \ + self.y, \ + self.height, \ + self.height, \ 360, \ 540 @@ -100,7 +100,8 @@ texture: self._helper_text_label.texture size: self._helper_text_label.texture_size pos: - self.x + (dp(16) if self.mode == "fill" else 0), \ + self.x + (dp(16) if self.mode == "fill" else \ + (0 if self.mode not in ("round", "rectangle") else dp(12))), \ self.y + (dp(-18) if self.mode in ("fill", "rectangle", "round") else dp(-2)) # Right/left icon texture. @@ -114,15 +115,22 @@ (self._icon_right_label.texture_size if self.icon_right else self._icon_left_label.texture_size) pos: ( \ - (self.width + self.x) - (self._icon_right_label.texture_size[1]) - dp(8), \ - self.center[1] - self._icon_right_label.texture_size[1] / 2 + ((dp(8) if self.mode != "round" else 0) if self.mode != "fill" else 0) \ + (self.width + self.x - (0 if self.mode != "round" else dp(4))) - \ + (self._icon_right_label.texture_size[1]) - dp(8), \ + self.center[1] - self._icon_right_label.texture_size[1] / 2 + ((dp(8) \ + if self.mode != "round" else 0) if self.mode != "fill" else 0) \ if self.mode != "rectangle" else \ self.center[1] - self._icon_right_label.texture_size[1] / 2 - dp(4) \ ) \ if self.icon_right else \ ( \ - self.x + (dp(0) if self.mode != "fill" else (dp(4) if not self.icon_left else dp(16))), \ - self.center[1] - self._icon_left_label.texture_size[1] / 2 + (((dp(4) if self.mode != "round" else 0) if self.mode not in ("rectangle", "fill") else dp(8)) if self.mode != "fill" else 0) \ + self.x + ((dp((0 if self.mode != "round" else 12)) \ + if self.mode != "rectangle" else dp(12)) \ + if self.mode != "fill" else (dp(4) if not self.icon_left else dp(16))), \ + + self.center[1] - self._icon_left_label.texture_size[1] / 2 + (((dp(4) \ + if self.mode != "round" else 0) if self.mode not in ("rectangle", "fill") \ + else dp(8)) if self.mode != "fill" else 0) \ if self.mode != "rectangle" else \ self.center[1] - self._icon_left_label.texture_size[1] / 2 - dp(4) \ ) @@ -134,7 +142,7 @@ texture: self._max_length_label.texture size: self._max_length_label.texture_size pos: - self.x + self.width - self._max_length_label.texture_size[0] - (dp(12) if self.mode != "round" else dp(0)), \ + self.x + self.width - self._max_length_label.texture_size[0] - dp(12), \ self.y - (dp(2) if self.mode == "line" else dp(18)) # Cursor blink. @@ -157,11 +165,16 @@ pos: ( \ self.x + ((dp(16) if not self.icon_left else dp(52)) \ - if self.mode == "fill" else (0 if not self.icon_left else \ - dp(36))) if not self.focus and not self.text else \ - self.x + ((dp(16) if self.mode != "round" else dp(36)) if self.mode in ("fill", "rectangle", "round") and \ - self.icon_left else 0) + self._hint_x + if self.mode == "fill" else ( \ + ((0 if self.mode != "round" else dp(12)) if self.mode != "rectangle" else dp(12)) \ + if not self.icon_left else \ + (dp(36 if self.mode != "round" else 42) if self.mode != "rectangle" else dp(42)))) \ + if not self.focus and not self.text else \ + self.x + ((dp(16) if self.mode != "round" else dp(36 if not self.icon_left else 42)) \ + if self.mode in ("fill", "rectangle", "round") and \ + self.icon_left else (0 if self.mode != "round" else dp(12))) + self._hint_x ), \ + self.y + self.height + (((dp(4) if self.mode != "round" else dp(10)) \ if self.mode != "line" else \ dp(-6)) if self.mode != "rectangle" else dp(-4)) - self._hint_y @@ -176,12 +189,14 @@ width: dp(1) if self.mode == "rectangle" else dp(0.00001) points: ( - self.x + self._line_blank_space_right_point, self.top - self._hint_text_label.texture_size[1] // 2, - self.right + dp(12), self.top - self._hint_text_label.texture_size[1] // 2, - self.right + dp(12), self.y, - self.x - dp(12), self.y, - self.x - dp(12), self.top - self._hint_text_label.texture_size[1] // 2, - self.x + self._line_blank_space_left_point, self.top - self._hint_text_label.texture_size[1] // 2 + self.x + self._line_blank_space_right_point, + self.top - self._hint_text_label.texture_size[1] // 2, + self.right, self.top - self._hint_text_label.texture_size[1] // 2, + self.right, self.y, + self.x, self.y, + self.x, self.top - self._hint_text_label.texture_size[1] // 2, + self.x + self._line_blank_space_left_point, + self.top - self._hint_text_label.texture_size[1] // 2 ) # Text color. @@ -197,10 +212,32 @@ foreground_color: self.theme_cls.text_color bold: False padding: - (0 if not self.icon_left else "36dp") if self.mode != "fill" else ("16dp" if not self.icon_left else "52dp"), \ + ( \ + ((0 if self.mode != "round" else "12dp") \ + if self.mode != "rectangle" else "12dp") \ + if not self.icon_left else \ + (("36dp" if self.mode != "round" else "42dp") \ + if self.mode != "rectangle" else "42dp") \ + ) \ + if self.mode != "fill" else \ + ("16dp" if not self.icon_left else "52dp"), \ + "24dp" if self.mode != "round" else "8dp", \ - 0 if self.mode != "fill" and not self.icon_right else ("14dp" if not self.icon_right else self._icon_right_label.texture_size[1] + dp(20)), \ - "8dp" if self.mode == "fill" else (("22dp" if self.mode != "round" else "8dp") if self.icon_left and self.mode != "rectangle" else ("16dp" if self.mode in ("fill", "rectangle") else "20dp" if self.mode != "round" else "8dp")) + + ((0 if self.mode != "round" and not self.icon_left else dp(12)) \ + if self.mode != "rectangle" else "12dp") \ + if self.mode != "fill" and not self.icon_right else \ + ( \ + "14dp" \ + if not self.icon_right else \ + self._icon_right_label.texture_size[1] + (dp(20) \ + if self.mode != "round" else dp(24))), \ + + "8dp" if self.mode == "fill" else \ + (("22dp" if self.mode != "round" else "8dp") \ + if self.icon_left and self.mode != "rectangle" else \ + ("16dp" if self.mode in ("fill", "rectangle") else \ + "20dp" if self.mode != "round" else "8dp")) multiline: False size_hint_y: None height: self.minimum_height diff --git a/sbapp/kivymd/uix/textfield/textfield.py b/sbapp/kivymd/uix/textfield/textfield.py index 727498b..08ea932 100755 --- a/sbapp/kivymd/uix/textfield/textfield.py +++ b/sbapp/kivymd/uix/textfield/textfield.py @@ -14,7 +14,6 @@ Components/TextField `KivyMD` provides the following field classes for use: - MDTextField_ -- MDTextFieldRound_ - MDTextFieldRect_ .. Note:: :class:`~MDTextField` inherited from @@ -79,15 +78,15 @@ parameter to `True`: from kivymd.app import MDApp KV = ''' - BoxLayout: - padding: "10dp" + MDScreen: MDTextField: id: text_field_error hint_text: "Helper text on error (press 'Enter')" helper_text: "There will always be a mistake" helper_text_mode: "on_error" - pos_hint: {"center_y": .5} + pos_hint: {"center_x": .5, "center_y": .5} + size_hint_x: .5 ''' @@ -97,6 +96,8 @@ parameter to `True`: self.screen = Builder.load_string(KV) def build(self): + self.theme_cls.theme_style = "Dark" + self.theme_cls.primary_palette = "Orange" self.screen.ids.text_field_error.bind( on_text_validate=self.set_error_message, on_focus=self.set_error_message, @@ -119,6 +120,7 @@ Helper text mode `'on_error'` (with required) MDTextField: hint_text: "required = True" + text: "required = True" required: True helper_text_mode: "on_error" helper_text: "Enter text" @@ -186,7 +188,7 @@ Round mode max_text_length: 15 helper_text: "Massage" -.. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/text-field-round-mode.png +.. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/text-field-round-mode.gif :align: center .. MDTextFieldRect: @@ -203,6 +205,7 @@ MDTextFieldRect MDTextFieldRect: size_hint: 1, None height: "30dp" + background_color: app.theme_cls.bg_normal .. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/text-field-rect.gif :align: center @@ -274,22 +277,21 @@ Clickable icon for MDTextField See more information in the :class:`~MDTextFieldRect` class. """ -__all__ = ("MDTextField", "MDTextFieldRect", "MDTextFieldRound") +__all__ = ("MDTextField", "MDTextFieldRect") import os import re +from datetime import date from typing import Union from kivy.animation import Animation from kivy.clock import Clock from kivy.lang import Builder -from kivy.logger import Logger from kivy.metrics import dp, sp from kivy.properties import ( AliasProperty, BooleanProperty, ColorProperty, - DictProperty, ListProperty, NumericProperty, ObjectProperty, @@ -302,6 +304,7 @@ from kivy.uix.textinput import TextInput from kivymd import uix_path from kivymd.font_definitions import theme_font_styles from kivymd.theming import ThemableBehavior +from kivymd.uix.behaviors import DeclarativeBehavior from kivymd.uix.label import MDIcon with open( @@ -310,6 +313,220 @@ with open( Builder.load_string(kv_file.read()) +# TODO: Add a class to work with the phone number mask. + + +class AutoFormatTelephoneNumber: + """ + Implements automatic formatting of the text entered in the text field + according to the mask, for example '+38 (###) ### ## ##'. + """ + + def __init__(self): + self._backspace = False + + def isnumeric(self, value): + try: + int(value) + return True + except ValueError: + return False + + def do_backspace(self, *args): + if self.validator and self.validator == "phone": + self._backspace = True + text = self.text + text = text[:-1] + self.text = text + self._backspace = False + + def field_filter(self, value, boolean): + if self.validator and self.validator == "phone": + if len(self.text) == 14: + return + if self.isnumeric(value): + return value + return value + + def format(self, value): + if value != "" and not value.isspace() and not self._backspace: + if len(value) <= 1 and self.focus: + self.text = value + self._check_cursor() + elif len(value) == 4: + start = self.text[:-1] + end = self.text[-1] + self.text = "%s) %s" % (start, end) + self._check_cursor() + elif len(value) == 8: + self.text += "-" + self._check_cursor() + elif len(value) in [12, 16]: + start = self.text[:-1] + end = self.text[-1] + self.text = "%s-%s" % (start, end) + self._check_cursor() + + def _check_cursor(self): + def set_pos_cursor(pos_corsor, interval=0.5): + self.cursor = (pos_corsor, 0) + + if self.focus: + Clock.schedule_once(lambda x: set_pos_cursor(len(self.text)), 0.1) + + +class Validator: + """Container class for various validation methods.""" + + datetime_date = ObjectProperty() + """ + The last valid date as a object. + + :attr:`datetime_date` is an :class:`~kivy.properties.ObjectProperty` + and defaults to `None`. + """ + + date_interval = ListProperty([None, None]) + """ + The date interval that is valid for input. + Can be entered as objects or a string format. + Both values or just one value can be entered. + + In string format, must follow the current date_format. + Example: Given date_format -> "mm/dd/yyyy" + Input examples -> "12/31/1900", "12/31/2100" or "12/31/1900", None. + + :attr:`date_interval` is an :class:`~kivy.properties.ListProperty` + and defaults to `[None, None]`. + """ + + date_format = OptionProperty( + None, + options=[ + "dd/mm/yyyy", + "mm/dd/yyyy", + "yyyy/mm/dd", + ], + ) + + """ + Format of date strings that will be entered. + Available options are: `'dd/mm/yyyy'`, `'mm/dd/yyyy'`, `'yyyy/mm/dd'`. + + :attr:`date_format` is an :class:`~kivy.properties.OptionProperty` + and defaults to `None`. + """ + + def is_email_valid(self, text: str) -> bool: + if not re.match(r"[^@]+@[^@]+\.[^@]+", text): + return True + return False + + def is_time_valid(self, text: str) -> bool: + if re.match(r"^(2[0-3]|[01]?[0-9]):([0-5]?[0-9])$", text) or re.match( + r"^(2[0-3]|[01]?[0-9]):([0-5]?[0-9]):([0-5]?[0-9])$", text + ): + return False + + return True + + def is_date_valid(self, text: str) -> bool: + if not self.date_format: + raise Exception("TextInput date_format was not defined.") + + # Regex strings. + dd = "[0][1-9]|[1-2][0-9]|[3][0-1]" + mm = "[0][1-9]|[1][0-2]" + yyyy = "[0-9][0-9][0-9][0-9]" + fmt = self.date_format.split("/") + largs = locals() + # Access the local variables dict in the correct format based on + # date_format split. Example: "mm/dd/yyyy" -> ["mm", "dd", "yyyy"] + # largs[fmt[0]] would be largs["mm"] so the month regex string. + if re.match( + f"^({largs[fmt[0]]})/({largs[fmt[1]]})/({largs[fmt[2]]})$", text + ): + input_split = text.split("/") + largs[fmt[0]] = input_split[0] + largs[fmt[1]] = input_split[1] + largs[fmt[2]] = input_split[2] + # Organize input into correct slots and try to convert + # to datetime object. This way February exceptions are + # tested. Also tests with the date_interval are simpler + # using datetime objects. + try: + datetime = date( + int(largs["yyyy"]), int(largs["mm"]), int(largs["dd"]) + ) + except ValueError: + return True + + if self.date_interval: + if ( + self.date_interval[0] + and not self.date_interval[0] <= datetime + or self.date_interval[1] + and not datetime <= self.date_interval[1] + ): + return True + + self.datetime_date = datetime + return False + return True + + def on_date_interval(self, *args) -> None: + """Default event handler for date_interval input.""" + + def on_date_interval(): + if not self.date_format: + raise Exception("TextInput date_format was not defined.") + + fmt = self.date_format.split("/") + largs = {} + # Convert string inputs into datetime.date objects and store + # them back into self.date_interval. + try: + if self.date_interval[0] and not isinstance( + self.date_interval[0], date + ): + split = self.date_interval[0].split("/") + largs[fmt[0]] = split[0] + largs[fmt[1]] = split[1] + largs[fmt[2]] = split[2] + self.date_interval[0] = date( + int(largs["yyyy"]), int(largs["mm"]), int(largs["dd"]) + ) + if self.date_interval[1] and not isinstance( + self.date_interval[1], date + ): + split = self.date_interval[1].split("/") + largs[fmt[0]] = split[0] + largs[fmt[1]] = split[1] + largs[fmt[2]] = split[2] + self.date_interval[1] = date( + int(largs["yyyy"]), int(largs["mm"]), int(largs["dd"]) + ) + + except Exception: + raise Exception( + r"TextInput date_interval was defined incorrectly, it must " + r"be composed of objects or strings" + r" following current date_format." + ) + + # Test if the interval is valid. + if isinstance(self.date_interval[0], date) and isinstance( + self.date_interval[1], date + ): + if self.date_interval[0] >= self.date_interval[1]: + raise Exception( + "TextInput date_interval last date must be greater" + " than the first date or set to None." + ) + + Clock.schedule_once(lambda x: on_date_interval()) + + class MDTextFieldRect(ThemableBehavior, TextInput): line_anim = BooleanProperty(True) """ @@ -382,7 +599,13 @@ class TextfieldLabel(ThemableBehavior, Label): self.font_size = sp(self.theme_cls.font_styles[self.font_style][1]) -class MDTextField(ThemableBehavior, TextInput): +class MDTextField( + DeclarativeBehavior, + ThemableBehavior, + TextInput, + Validator, + AutoFormatTelephoneNumber, +): helper_text = StringProperty() """ Text for ``helper_text`` mode. @@ -418,20 +641,6 @@ class MDTextField(ThemableBehavior, TextInput): and defaults to `False`. """ - color_mode = OptionProperty( - "primary", options=["primary", "accent", "custom"], deprecated=True - ) - """ - Color text mode. Available options are: `'primary'`, `'accent'`, - `'custom'`. - - .. deprecated:: 1.0.0 - Don't use this attribute. - - :attr:`color_mode` is an :class:`~kivy.properties.OptionProperty` - and defaults to `'primary'`. - """ - mode = OptionProperty( "line", options=["rectangle", "round", "fill", "line"] ) @@ -443,17 +652,185 @@ class MDTextField(ThemableBehavior, TextInput): and defaults to `'line'`. """ + phone_mask = StringProperty("") + + validator = OptionProperty(None, options=["date", "email", "time", "phone"]) + """ + The type of text field for entering Email, time, etc. + Automatically sets the type of the text field as "error" if the user input + does not match any of the set validation types. + Available options are: `'date'`, `'email'`, `'time'`. + + When using `'date'`, :attr:`date_format` must be defined. + + .. versionadded:: 1.1.0 + + .. code-block:: python + + MDTextField: + hint_text: "Email" + helper_text: "user@gmail.com" + validator: "email" + + .. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/text-field-validator.png + :align: center + + .. tabs:: + + .. tab:: Declarative KV style + + .. code-block:: python + + from kivy.lang import Builder + + from kivymd.app import MDApp + + KV = ''' + MDScreen: + + MDBoxLayout: + orientation: "vertical" + spacing: "20dp" + adaptive_height: True + size_hint_x: .8 + pos_hint: {"center_x": .5, "center_y": .5} + + MDTextField: + hint_text: "Date dd/mm/yyyy without limits" + helper_text: "Enter a valid dd/mm/yyyy date" + validator: "date" + date_format: "dd/mm/yyyy" + + MDTextField: + hint_text: "Date mm/dd/yyyy without limits" + helper_text: "Enter a valid mm/dd/yyyy date" + validator: "date" + date_format: "mm/dd/yyyy" + + MDTextField: + hint_text: "Date yyyy/mm/dd without limits" + helper_text: "Enter a valid yyyy/mm/dd date" + validator: "date" + date_format: "yyyy/mm/dd" + + MDTextField: + hint_text: "Date dd/mm/yyyy in [01/01/1900, 01/01/2100] interval" + helper_text: "Enter a valid dd/mm/yyyy date" + validator: "date" + date_format: "dd/mm/yyyy" + date_interval: "01/01/1900", "01/01/2100" + + MDTextField: + hint_text: "Date dd/mm/yyyy in [01/01/1900, None] interval" + helper_text: "Enter a valid dd/mm/yyyy date" + validator: "date" + date_format: "dd/mm/yyyy" + date_interval: "01/01/1900", None + + MDTextField: + hint_text: "Date dd/mm/yyyy in [None, 01/01/2100] interval" + helper_text: "Enter a valid dd/mm/yyyy date" + validator: "date" + date_format: "dd/mm/yyyy" + date_interval: None, "01/01/2100" + ''' + + + class Test(MDApp): + def build(self): + self.theme_cls.theme_style = "Dark" + self.theme_cls.primary_palette = "Orange" + return Builder.load_string(KV) + + + Test().run() + + .. tab:: Declarative python style + + .. code-block:: python + + from kivymd.app import MDApp + from kivymd.uix.boxlayout import MDBoxLayout + from kivymd.uix.screen import MDScreen + from kivymd.uix.textfield import MDTextField + + + class Test(MDApp): + def build(self): + self.theme_cls.theme_style = "Dark" + self.theme_cls.primary_palette = "Orange" + return ( + MDScreen( + MDBoxLayout( + MDTextField( + hint_text="Date dd/mm/yyyy without limits", + helper_text="Enter a valid dd/mm/yyyy date", + validator="date", + date_format="dd/mm/yyyy", + ), + MDTextField( + hint_text="Date mm/dd/yyyy without limits", + helper_text="Enter a valid mm/dd/yyyy date", + validator="date", + date_format="mm/dd/yyyy", + ), + MDTextField( + hint_text="Date yyyy/mm/dd without limits", + helper_text="Enter a valid yyyy/mm/dd date", + validator="date", + date_format="yyyy/mm/dd", + ), + MDTextField( + hint_text="Date dd/mm/yyyy in [01/01/1900, 01/01/2100] interval", + helper_text="Enter a valid dd/mm/yyyy date", + validator="date", + date_format="dd/mm/yyyy", + date_interval=["01/01/1900", "01/01/2100"], + ), + MDTextField( + hint_text="Date dd/mm/yyyy in [01/01/1900, None] interval", + helper_text="Enter a valid dd/mm/yyyy date", + validator="date", + date_format="dd/mm/yyyy", + date_interval=["01/01/1900", None], + ), + MDTextField( + hint_text="Date dd/mm/yyyy in [None, 01/01/2100] interval", + helper_text="Enter a valid dd/mm/yyyy date", + validator="date", + date_format="dd/mm/yyyy", + date_interval=[None, "01/01/2100"], + ), + orientation="vertical", + spacing="20dp", + adaptive_height=True, + size_hint_x=0.8, + pos_hint={"center_x": 0.5, "center_y": 0.5}, + ) + ) + ) + + + Test().run() + + .. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/text-field-validator-date.png + :align: center + + :attr:`validator` is an :class:`~kivy.properties.OptionProperty` + and defaults to `None`. + """ + line_color_normal = ColorProperty([0, 0, 0, 0]) """ - Line color normal (static underline line) in ``rgba`` format. + Line color normal (static underline line) in (r, g, b, a) or string format. .. code-block:: kv MDTextField: hint_text: "line_color_normal" - line_color_normal: 1, 0, 1, 1 + line_color_normal: "red" - .. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/text-field-line-color-normal.gif + .. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/text-field-line-color-normal.png :align: center :attr:`line_color_normal` is an :class:`~kivy.properties.ColorProperty` @@ -462,13 +839,13 @@ class MDTextField(ThemableBehavior, TextInput): line_color_focus = ColorProperty([0, 0, 0, 0]) """ - Line color focus (active underline line) in ``rgba`` format. + Line color focus (active underline line) in (r, g, b, a) or string format. .. code-block:: kv MDTextField: hint_text: "line_color_focus" - line_color_focus: 0, 1, 0, 1 + line_color_focus: "red" .. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/text-field-line-color-focus.gif :align: center @@ -487,27 +864,26 @@ class MDTextField(ThemableBehavior, TextInput): error_color = ColorProperty([0, 0, 0, 0]) """ - Error color in ``rgba`` format for ``required = True``. + Error color in (r, g, b, a) or string format for ``required = True``. :attr:`error_color` is an :class:`~kivy.properties.ColorProperty` and defaults to `[0, 0, 0, 0]`. """ - fill_color = ColorProperty([0, 0, 0, 0], deprecated=True) - """ - The background color of the fill in rgba format when the ``mode`` parameter - is "fill". - - .. deprecated:: 1.0.0 - Use :attr:`fill_color_normal` and :attr:`fill_color_focus` instead. - - :attr:`fill_color` is an :class:`~kivy.properties.ColorProperty` - and defaults to `[0, 0, 0, 0]`. - """ - fill_color_normal = ColorProperty([0, 0, 0, 0]) """ - Fill background color in 'fill' mode when text field is out of focus. + Fill background color in (r, g, b, a) or string format in 'fill' mode when] + text field is out of focus. + + .. code=block:: kv + + MDTextField: + hint_text: "Fill mode" + mode: "fill" + fill_color_normal: "brown" + + .. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/text-field-fill-color-normal.png + :align: center :attr:`fill_color_normal` is an :class:`~kivy.properties.ColorProperty` and defaults to `[0, 0, 0, 0]`. @@ -515,7 +891,18 @@ class MDTextField(ThemableBehavior, TextInput): fill_color_focus = ColorProperty([0, 0, 0, 0]) """ - Fill background color in 'fill' mode when the text field has focus. + Fill background color in (r, g, b, a) or string format in 'fill' mode when + the text field has focus. + + .. code=block:: kv + + MDTextField: + hint_text: "Fill mode" + mode: "fill" + fill_color_focus: "brown" + + .. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/text-field-fill-color-focus.gif + :align: center :attr:`fill_color_focus` is an :class:`~kivy.properties.ColorProperty` and defaults to `[0, 0, 0, 0]`. @@ -537,20 +924,10 @@ class MDTextField(ThemableBehavior, TextInput): and defaults to `False`. """ - current_hint_text_color = ColorProperty([0, 0, 0, 0], deprecated=True) - """ - Hint text color. - - .. deprecated:: 1.0.0 - Use :attr:`hint_text_color_normal` and :attr:`hint_text_color_focus` instead. - - :attr:`current_hint_text_color` is an :class:`~kivy.properties.ColorProperty` - and defaults to `[0, 0, 0, 0]`. - """ - hint_text_color_normal = ColorProperty([0, 0, 0, 0]) """ - Hint text color when text field is out of focus. + Hint text color in (r, g, b, a) or string format when text field is out + of focus. .. versionadded:: 1.0.0 @@ -558,9 +935,9 @@ class MDTextField(ThemableBehavior, TextInput): MDTextField: hint_text: "hint_text_color_normal" - hint_text_color_normal: 0, 1, 0, 1 + hint_text_color_normal: "red" - .. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/text-field-hint-text-color-normal.gif + .. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/text-field-hint-text-color-normal.png :align: center :attr:`hint_text_color_normal` is an :class:`~kivy.properties.ColorProperty` @@ -569,7 +946,8 @@ class MDTextField(ThemableBehavior, TextInput): hint_text_color_focus = ColorProperty([0, 0, 0, 0]) """ - Hint text color when the text field has focus. + Hint text color in (r, g, b, a) or string format when the text field has + focus. .. versionadded:: 1.0.0 @@ -577,7 +955,7 @@ class MDTextField(ThemableBehavior, TextInput): MDTextField: hint_text: "hint_text_color_focus" - hint_text_color_focus: 0, 1, 0, 1 + hint_text_color_focus: "red" .. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/text-field-hint-text-color-focus.gif :align: center @@ -588,7 +966,8 @@ class MDTextField(ThemableBehavior, TextInput): helper_text_color_normal = ColorProperty([0, 0, 0, 0]) """ - Helper text color when text field is out of focus. + Helper text color in (r, g, b, a) or string format when text field is out + of focus. .. versionadded:: 1.0.0 @@ -597,7 +976,7 @@ class MDTextField(ThemableBehavior, TextInput): MDTextField: helper_text: "helper_text_color_normal" helper_text_mode: "persistent" - helper_text_color_normal: 0, 1, 0, 1 + helper_text_color_normal: "red" .. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/text-field-helper-text-color-normal.png :align: center @@ -608,7 +987,8 @@ class MDTextField(ThemableBehavior, TextInput): helper_text_color_focus = ColorProperty([0, 0, 0, 0]) """ - Helper text color when the text field has focus. + Helper text color in (r, g, b, a) or string format when the text field has + focus. .. versionadded:: 1.0.0 @@ -617,7 +997,7 @@ class MDTextField(ThemableBehavior, TextInput): MDTextField: helper_text: "helper_text_color_focus" helper_text_mode: "persistent" - helper_text_color_focus: 0, 1, 0, 1 + helper_text_color_focus: "red" .. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/text-field-helper-text-color-focus.gif :align: center @@ -628,7 +1008,8 @@ class MDTextField(ThemableBehavior, TextInput): icon_right_color_normal = ColorProperty([0, 0, 0, 0]) """ - Color of right icon when text field is out of focus. + Color in (r, g, b, a) or string format of right icon when text field is out + of focus. .. versionadded:: 1.0.0 @@ -637,9 +1018,9 @@ class MDTextField(ThemableBehavior, TextInput): MDTextField: icon_right: "language-python" hint_text: "icon_right_color_normal" - icon_right_color_normal: 0, 1, 0, 1 + icon_right_color_normal: "red" - .. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/text-field-icon-right-color-normal.gif + .. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/text-field-icon-right-color-normal.png :align: center :attr:`icon_right_color_normal` is an :class:`~kivy.properties.ColorProperty` @@ -648,7 +1029,8 @@ class MDTextField(ThemableBehavior, TextInput): icon_right_color_focus = ColorProperty([0, 0, 0, 0]) """ - Color of right icon when the text field has focus. + Color in (r, g, b, a) or string format of right icon when the text field + has focus. .. versionadded:: 1.0.0 @@ -657,7 +1039,7 @@ class MDTextField(ThemableBehavior, TextInput): MDTextField: icon_right: "language-python" hint_text: "icon_right_color_focus" - icon_right_color_focus: 0, 1, 0, 1 + icon_right_color_focus: "red" .. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/text-field-icon-right-color-focus.gif :align: center @@ -668,47 +1050,30 @@ class MDTextField(ThemableBehavior, TextInput): icon_left_color_normal = ColorProperty([0, 0, 0, 0]) """ - Color of right icon when text field is out of focus. + Color in (r, g, b, a) or string format of right icon when text field is out + of focus. .. versionadded:: 1.0.0 - .. code-block:: kv - - MDTextField: - icon_right: "language-python" - hint_text: "icon_right_color_normal" - icon_left_color_normal: 0, 1, 0, 1 - - .. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/text-field-icon-right-color-normal.gif - :align: center - :attr:`icon_left_color_normal` is an :class:`~kivy.properties.ColorProperty` and defaults to `[0, 0, 0, 0]`. """ icon_left_color_focus = ColorProperty([0, 0, 0, 0]) """ - Color of right icon when the text field has focus. + Color in (r, g, b, a) or string format of right icon when the text field + has focus. .. versionadded:: 1.0.0 - .. code-block:: kv - - MDTextField: - icon_right: "language-python" - hint_text: "icon_right_color_focus" - icon_right_color_focus: 0, 1, 0, 1 - - .. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/text-field-icon-right-color-focus.gif - :align: center - :attr:`icon_left_color_focus` is an :class:`~kivy.properties.ColorProperty` and defaults to `[0, 0, 0, 0]`. """ max_length_text_color = ColorProperty([0, 0, 0, 0]) """ - Text color of the maximum length of characters to be input. + Text color in (r, g, b, a) or string format of the maximum length of + characters to be input. .. versionadded:: 1.0.0 @@ -716,10 +1081,10 @@ class MDTextField(ThemableBehavior, TextInput): MDTextField: hint_text: "max_length_text_color" - max_length_text_color: 0, 1, 0, 1 + max_length_text_color: "red" max_text_length: 5 - .. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/text-field-max-length-text-color.gif + .. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/text-field-max-length-text-color.png :align: center :attr:`max_length_text_color` is an :class:`~kivy.properties.ColorProperty` @@ -752,31 +1117,9 @@ class MDTextField(ThemableBehavior, TextInput): and defaults to `''`. """ - icon_right_color = ColorProperty([0, 0, 0, 1], deprecated=True) - """ - Color of right icon in ``rgba`` format. - - .. deprecated:: 1.0.0 - Don't use this attribute. - - :attr:`icon_right_color` is an :class:`~kivy.properties.ColorProperty` - and defaults to `[0, 0, 0, 1]`. - """ - - text_color = ColorProperty([0, 0, 0, 0], deprecated=True) - """ - Text color in ``rgba`` format. - - .. deprecated:: 1.0.0 - Use :attr:`text_color_normal` and :attr:`text_color_focus` instead. - - :attr:`text_color` is an :class:`~kivy.properties.ColorProperty` - and defaults to `[0, 0, 0, 0]`. - """ - text_color_normal = ColorProperty([0, 0, 0, 0]) """ - Text color in ``rgba`` format when text field is out of focus. + Text color in (r, g, b, a) or string format when text field is out of focus. .. versionadded:: 1.0.0 @@ -784,9 +1127,9 @@ class MDTextField(ThemableBehavior, TextInput): MDTextField: hint_text: "text_color_normal" - text_color_normal: 0, 1, 0, 1 + text_color_normal: "red" - .. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/text-field-text-color-normal.gif + .. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/text-field-text-color-normal.png :align: center :attr:`text_color_normal` is an :class:`~kivy.properties.ColorProperty` @@ -795,7 +1138,7 @@ class MDTextField(ThemableBehavior, TextInput): text_color_focus = ColorProperty([0, 0, 0, 0]) """ - Text color in ``rgba`` format when text field has focus. + Text color in (r, g, b, a) or string format when text field has focus. .. versionadded:: 1.0.0 @@ -803,7 +1146,7 @@ class MDTextField(ThemableBehavior, TextInput): MDTextField: hint_text: "text_color_focus" - text_color_focus: 0, 1, 0, 1 + text_color_focus: "red" .. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/text-field-text-color-focus.gif :align: center @@ -924,12 +1267,12 @@ class MDTextField(ThemableBehavior, TextInput): # application color palette. _colors_to_updated = ListProperty() - def __init__(self, **kwargs): + def __init__(self, *args, **kwargs): self.set_objects_labels() Clock.schedule_once(self._set_attr_names_to_updated) Clock.schedule_once(self.set_colors_to_updated) Clock.schedule_once(self.set_default_colors) - super().__init__(**kwargs) + super().__init__(*args, **kwargs) self.bind( _hint_text_font_size=self._hint_text_label.setter("font_size"), _icon_right_color=self._icon_right_label.setter("text_color"), @@ -937,8 +1280,8 @@ class MDTextField(ThemableBehavior, TextInput): text=self.set_text, ) self.theme_cls.bind( - primary_color=lambda x, y: self.set_default_colors(0, True), - theme_style=lambda x, y: self.set_default_colors(0, True), + primary_color=self.set_default_colors, + theme_style=self.set_default_colors, ) Clock.schedule_once(self.check_text) @@ -988,9 +1331,17 @@ class MDTextField(ThemableBehavior, TextInput): ) if self.error_color == [0, 0, 0, 0] or updated: - self.error_color = self.theme_cls.error_color + self.error_color = ( + self.theme_cls.error_color + if self.error_color == [0, 0, 0, 0] + else self.error_color + ) if self.max_length_text_color == [0, 0, 0, 0] or updated: - self.max_length_text_color = self.theme_cls.disabled_hint_text_color + self.max_length_text_color = ( + self.theme_cls.disabled_hint_text_color + if self.max_length_text_color == [0, 0, 0, 0] + else self.max_length_text_color + ) self._hint_text_color = self.hint_text_color_normal self._text_color_normal = self.text_color_normal @@ -1015,12 +1366,12 @@ class MDTextField(ThemableBehavior, TextInput): def on_progress(*args): self._line_blank_space_right_point = ( - self._hint_text_label.width + dp(5) if not joining else 0 + self._hint_text_label.width + dp(17) if not joining else 0 ) if self.hint_text: animation = Animation( - _line_blank_space_left_point=self._hint_text_label.x - dp(5) + _line_blank_space_left_point=self._hint_text_label.x - dp(-7) if not joining else 0, duration=0.2, @@ -1103,7 +1454,7 @@ class MDTextField(ThemableBehavior, TextInput): t="out_quad", ).start(self) - def set_pos_hint_text(self, y: float, x: float = 0) -> None: + def set_pos_hint_text(self, y: float, x: float = 12) -> None: """Animates the x-axis width and y-axis height of the hint text.""" if self.mode != "round": @@ -1113,7 +1464,7 @@ class MDTextField(ThemableBehavior, TextInput): _hint_x = x else: if y == dp(10): - _hint_x = dp(-16) + _hint_x = dp(-4) else: _hint_x = dp(20) @@ -1159,8 +1510,11 @@ class MDTextField(ThemableBehavior, TextInput): self.text = re.sub("\n", " ", text) if not self.multiline else text self.set_max_text_length() + if self.validator and self.validator == "phone": + pass + # self.format(self.text) - if self.text and self.max_length_text_color and self._get_has_error(): + if (self.text and self.max_length_text_color) or self._get_has_error(): self.error = True if ( self.text @@ -1359,22 +1713,34 @@ class MDTextField(ThemableBehavior, TextInput): if value_height >= self.max_height and self.max_height: self.height = self.max_height - def on_text_color_normal(self, instance_text_field, color: list): + def on_text_color_normal( + self, instance_text_field, color: Union[list, str] + ): self._text_color_normal = color - def on_hint_text_color_normal(self, instance_text_field, color: list): + def on_hint_text_color_normal( + self, instance_text_field, color: Union[list, str] + ): self._hint_text_color = color - def on_helper_text_color_normal(self, instance_text_field, color: list): + def on_helper_text_color_normal( + self, instance_text_field, color: Union[list, str] + ): self._helper_text_color = color - def on_icon_right_color_normal(self, instance_text_field, color: list): + def on_icon_right_color_normal( + self, instance_text_field, color: Union[list, str] + ): self._icon_right_color = color - def on_line_color_normal(self, instance_text_field, color: list): + def on_line_color_normal( + self, instance_text_field, color: Union[list, str] + ): self._line_color_normal = color - def on_max_length_text_color(self, instance_text_field, color: list): + def on_max_length_text_color( + self, instance_text_field, color: Union[list, str] + ): self._max_length_text_color = color def _set_color(self, attr_name: str, color: str, updated: bool) -> None: @@ -1411,6 +1777,13 @@ class MDTextField(ThemableBehavior, TextInput): the :attr:`~MDTextField.required` parameter is set to `True`. """ + if self.validator and self.validator != "phone": + has_error = { + "date": self.is_date_valid, + "email": self.is_email_valid, + "time": self.is_time_valid, + }[self.validator](self.text) + return has_error if self.max_text_length and len(self.text) > self.max_text_length: has_error = True else: @@ -1424,25 +1797,13 @@ class MDTextField(ThemableBehavior, TextInput): """Method override to avoid duplicate hint text texture.""" -class MDTextFieldRound(MDTextField): - """ - .. deprecated:: 1.0.0 - Use :class:`~MDTextField` class instead. - """ - - def __init__(self, **kwargs): - super().__init__(**kwargs) - Logger.warning( - "KivyMD: " - "The `MDTextFieldRound` class has been deprecated. " - "Use the `MDTextField` class instead with `mode='round'`" - ) - - if __name__ == "__main__": + from kivy.core.window import Window from kivy.lang import Builder from kivy.uix.textinput import TextInput + Window.size = (800, 750) + from kivymd.app import MDApp KV = """ @@ -1458,41 +1819,53 @@ MDScreen: MDTextField: hint_text: "Label" - helper_text: "Error massage" + helper_text: "Error message" mode: "rectangle" max_text_length: 5 MDTextField: icon_left: "git" hint_text: "Label" - helper_text: "Error massage" + helper_text: "Error message" mode: "rectangle" MDTextField: icon_left: "git" hint_text: "Label" - helper_text: "Error massage" + helper_text: "Error message" mode: "fill" MDTextField: hint_text: "Label" - helper_text: "Error massage" + helper_text: "Error message" mode: "fill" MDTextField: hint_text: "Label" - helper_text: "Error massage" + helper_text: "Error message" MDTextField: icon_left: "git" hint_text: "Label" - helper_text: "Error massage" + helper_text: "Error message" MDTextField: hint_text: "Round mode" mode: "round" max_text_length: 15 - helper_text: "Massage" + helper_text: "Message" + + MDTextField: + hint_text: "Date dd/mm/yyyy in [01/01/1900, 01/01/2100] interval" + helper_text: "Enter a valid dd/mm/yyyy date" + validator: "date" + date_format: "dd/mm/yyyy" + date_interval: "01/01/1900", "01/01/2100" + + MDTextField: + hint_text: "Email" + helper_text: "user@gmail.com" + validator: "email" MDFlatButton: text: "SET TEXT" @@ -1502,6 +1875,8 @@ MDScreen: class Test(MDApp): def build(self): + self.theme_cls.theme_style = "Dark" + self.theme_cls.primary_palette = "Orange" return Builder.load_string(KV) def set_text(self): diff --git a/sbapp/kivymd/uix/toolbar/__init__.py b/sbapp/kivymd/uix/toolbar/__init__.py index 15a8177..d6fa24d 100644 --- a/sbapp/kivymd/uix/toolbar/__init__.py +++ b/sbapp/kivymd/uix/toolbar/__init__.py @@ -1,2 +1,2 @@ # NOQA F401 -from .toolbar import MDBottomAppBar, MDToolbar, MDTopAppBar +from .toolbar import MDBottomAppBar, MDTopAppBar diff --git a/sbapp/kivymd/uix/toolbar/toolbar.kv b/sbapp/kivymd/uix/toolbar/toolbar.kv index 2ea68f7..b5538af 100644 --- a/sbapp/kivymd/uix/toolbar/toolbar.kv +++ b/sbapp/kivymd/uix/toolbar/toolbar.kv @@ -24,8 +24,11 @@ if not self.md_bg_bottom_color else \ self.md_bg_bottom_color \ ) \ - if root.parent.md_bg_color == [0, 0, 0, 0] \ - else root.parent.md_bg_color \ + if root.parent and root.parent.md_bg_color == [0, 0, 0, 0] \ + else \ + ( \ + root.parent.md_bg_color if root.parent else root.md_bg_color \ + ) \ ) Mesh: vertices: root._vertices_left diff --git a/sbapp/kivymd/uix/toolbar/toolbar.py b/sbapp/kivymd/uix/toolbar/toolbar.py index 84c5492..0a48548 100755 --- a/sbapp/kivymd/uix/toolbar/toolbar.py +++ b/sbapp/kivymd/uix/toolbar/toolbar.py @@ -121,8 +121,8 @@ Shadow elevation control .. code-block:: kv MDTopAppBar: - title: "Elevation 10" - elevation: 10 + title: "Elevation 4" + elevation: 4 .. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/toolbar-7.png :align: center @@ -327,7 +327,7 @@ Material design 3 style :align: center """ -__all__ = ("MDTopAppBar", "MDBottomAppBar", "MDToolbar") +__all__ = ("MDTopAppBar", "MDBottomAppBar", "ActionTopAppBarButton") import os from math import cos, radians, sin @@ -337,10 +337,8 @@ from kivy.animation import Animation from kivy.clock import Clock from kivy.core.window import Window from kivy.lang import Builder -from kivy.logger import Logger from kivy.metrics import dp from kivy.properties import ( - AliasProperty, BooleanProperty, ColorProperty, ListProperty, @@ -356,14 +354,15 @@ from kivymd import uix_path from kivymd.color_definitions import text_colors from kivymd.theming import ThemableBehavior from kivymd.uix.behaviors import ( - FakeRectangularElevationBehavior, + CommonElevationBehavior, + DeclarativeBehavior, + ScaleBehavior, SpecificBackgroundColorBehavior, ) from kivymd.uix.button import MDFloatingActionButton, MDIconButton from kivymd.uix.controllers import WindowController from kivymd.uix.list import OneLineIconListItem from kivymd.uix.menu import MDDropdownMenu -from kivymd.uix.templates import ScaleWidget from kivymd.uix.tooltip import MDTooltip from kivymd.utils.set_bars_colors import set_bars_colors @@ -373,7 +372,7 @@ with open( Builder.load_string(kv_file.read()) -class ActionBottomAppBarButton(MDFloatingActionButton, ScaleWidget): +class ActionBottomAppBarButton(MDFloatingActionButton, ScaleBehavior): """ Implements a floating action button (FAB) for a toolbar with type 'bottom'. """ @@ -408,11 +407,11 @@ class OverFlowMenuItem(OneLineIconListItem): class NotchedBox( ThemableBehavior, - FakeRectangularElevationBehavior, + CommonElevationBehavior, SpecificBackgroundColorBehavior, BoxLayout, ): - elevation = NumericProperty(6) + elevation = NumericProperty(4) notch_radius = NumericProperty() notch_center_x = NumericProperty("100dp") @@ -555,7 +554,7 @@ class NotchedBox( return points -class MDTopAppBar(NotchedBox, WindowController): +class MDTopAppBar(DeclarativeBehavior, NotchedBox, WindowController): """ :Events: `on_action_button` @@ -945,18 +944,6 @@ class MDTopAppBar(NotchedBox, WindowController): and defaults to `'small'`. """ - round = NumericProperty("10dp", deprecated=True) - """ - Rounding the corners at the notch for a button. - Only for :class:`~MDBottomAppBar` class. - - .. deprecated:: 1.0.0 - Don't use this attribute. - - :attr:`round` is an :class:`~kivy.properties.NumericProperty` - and defaults to `'10dp'`. - """ - # List of action buttons (ActionTopAppBarButton instance) that have been # .added to the overflow _hidden_items = [] @@ -972,8 +959,10 @@ class MDTopAppBar(NotchedBox, WindowController): self.icon_color = self.theme_cls.primary_color self.bind(specific_text_color=self.update_action_bar_text_colors) - self.theme_cls.bind(material_style=self.update_bar_height) - self.theme_cls.bind(primary_palette=self.update_md_bg_color) + self.theme_cls.bind( + material_style=self.update_bar_height, + primary_palette=self.update_md_bg_color, + ) Clock.schedule_once( lambda x: self.on_left_action_items(0, self.left_action_items) @@ -1114,6 +1103,7 @@ class MDTopAppBar(NotchedBox, WindowController): + self.theme_cls.standard_increment / 2 + self._shift ) + self.shadow_offset = [0, 30] self.on_mode(None, self.mode) def on_type_height(self, instance_toolbar, height_type_value: str) -> None: @@ -1309,7 +1299,8 @@ class MDTopAppBar(NotchedBox, WindowController): if material_style_value == "M2": self.anchor_title = "left" elif material_style_value == "M3" and self.type != "bottom": - self.anchor_title = "center" + if not self.anchor_title: + self.anchor_title = "center" elif material_style_value == "M3" and self.type == "bottom": self.anchor_title = "left" return self.anchor_title @@ -1375,7 +1366,7 @@ class MDTopAppBar(NotchedBox, WindowController): ][self.theme_cls.primary_hue] -class MDBottomAppBar(FloatLayout): +class MDBottomAppBar(DeclarativeBehavior, FloatLayout): md_bg_color = ColorProperty([0, 0, 0, 0]) """ Color toolbar. @@ -1384,27 +1375,11 @@ class MDBottomAppBar(FloatLayout): and defaults to `[0, 0, 0, 0]`. """ - def __init__(self, **kwargs): - super().__init__(**kwargs) + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) self.size_hint_y = None def add_widget(self, widget, index=0, canvas=None): if isinstance(widget, MDTopAppBar): super().add_widget(widget) return super().add_widget(widget.action_button) - - -class MDToolbar(MDTopAppBar): - """ - .. deprecated:: 1.0.0 - - Use :class:`~MDTopAppBar` class instead. - """ - - def __init__(self, **kwargs): - super().__init__(**kwargs) - Logger.warning( - "KivyMD: " - "The `MDToolbar` class has been deprecated. Use the `MDTopAppBar` " - "class instead." - ) diff --git a/sbapp/kivymd/uix/tooltip/tooltip.py b/sbapp/kivymd/uix/tooltip/tooltip.py index 3c88333..855119a 100644 --- a/sbapp/kivymd/uix/tooltip/tooltip.py +++ b/sbapp/kivymd/uix/tooltip/tooltip.py @@ -279,7 +279,7 @@ class MDTooltip(ThemableBehavior, HoverBehavior, TouchBehavior): def on_long_touch(self, touch, *args) -> None: if DEVICE_TYPE != "desktop": - self.on_enter(True) + self.on_enter() def on_enter(self, *args) -> None: """ @@ -289,16 +289,18 @@ class MDTooltip(ThemableBehavior, HoverBehavior, TouchBehavior): class. """ - if not args and DEVICE_TYPE == "desktop": - if self.tooltip_text: - self._tooltip = MDTooltipViewClass( - tooltip_bg_color=self.tooltip_bg_color, - tooltip_text_color=self.tooltip_text_color, - tooltip_text=self.tooltip_text, - tooltip_font_style=self.tooltip_font_style, - tooltip_radius=self.tooltip_radius, - ) - Clock.schedule_once(self.display_tooltip, -1) + if self.tooltip_text: + if self._tooltip: + self.remove_tooltip() + + self._tooltip = MDTooltipViewClass( + tooltip_bg_color=self.tooltip_bg_color, + tooltip_text_color=self.tooltip_text_color, + tooltip_text=self.tooltip_text, + tooltip_font_style=self.tooltip_font_style, + tooltip_radius=self.tooltip_radius, + ) + Clock.schedule_once(self.display_tooltip, -1) def on_leave(self) -> None: """ @@ -312,7 +314,7 @@ class MDTooltip(ThemableBehavior, HoverBehavior, TouchBehavior): Clock.schedule_once(self.animation_tooltip_dismiss) def on_show(self) -> None: - """Default dismiss event handler.""" + """Default display event handler.""" def on_dismiss(self) -> None: """ diff --git a/sbapp/kivymd/uix/transition/transition.py b/sbapp/kivymd/uix/transition/transition.py index 49bd657..169584d 100644 --- a/sbapp/kivymd/uix/transition/transition.py +++ b/sbapp/kivymd/uix/transition/transition.py @@ -35,7 +35,9 @@ __all__ = ( "MDTransitionBase", ) +from kivy import Logger from kivy.animation import Animation, AnimationTransition +from kivy.properties import DictProperty from kivy.uix.screenmanager import ( ScreenManagerException, SlideTransition, @@ -43,13 +45,41 @@ from kivy.uix.screenmanager import ( TransitionBase, ) +from kivymd.uix.hero import MDHeroFrom, MDHeroTo from kivymd.uix.screenmanager import MDScreenManager class MDTransitionBase(TransitionBase): + """ + TransitionBase is used to animate 2 screens within the + :class:`~kivymd.uix.screenmanager.MDScreenManager`. + + For more + information, see in the :class:`~kivy.uix.screenmanager.TransitionBase` + class documentation. + """ + _direction = "in" - hero_widget = None - hero_from_widget = None # kivymd.uix.hero.MDHeroFrom object + # Collection of child widgets of all 'MDHeroFrom' widgets that are + # on the screen, for example: + # + # MDScreen: + # + # MDHeroFrom: + # tag: "kivymd" + # + # FitImage: + # + # MDHeroFrom: + # tag: "kivy" + # + # FitImage: + # + # { + # 'kivy': , + # 'kivymd': , + # } + _hero_from_widget_children = DictProperty() def start(self, instance_screen_manager: MDScreenManager) -> None: super().start(instance_screen_manager) @@ -59,67 +89,160 @@ class MDTransitionBase(TransitionBase): ]() def animated_hero_in(self) -> None: - if self.manager._heroes_data and self.manager.current_hero: - self.hero_from_widget = self.manager.get_hero_from_widget() - self._check_widget_properties() - self.hero_widget = self.hero_from_widget.children[0] - self.hero_from_widget.remove_widget(self.hero_widget) + """Animates the flight of heroes from screen **A** to screen **B**.""" - self.hero_widget.pos = self.screen_out.to_widget( - *self.hero_from_widget.to_window(*self.hero_from_widget.pos) - ) - self.hero_widget.size = self.hero_from_widget.size - self.manager.get_root_window().add_widget(self.hero_widget) + if self.manager._heroes_data and self.manager.current_heroes: + for hero_from_widget in self.manager.get_hero_from_widget(): + for heroes_tag in self.manager.current_heroes: + if heroes_tag == hero_from_widget.tag: + self._check_widget_properties(hero_from_widget) - Animation( - size=self.screen_in.hero_to.size, - d=self.duration, - pos=self.screen_in.hero_to.pos, - ).start(self.hero_widget) - self.hero_from_widget.dispatch( - "on_transform_in", self.hero_widget, self.duration - ) + # Get child widget of the 'MDHeroFrom' container. + hero_widget = hero_from_widget.children[0] + self._hero_from_widget_children[ + hero_from_widget.tag + ] = hero_widget + + # Removing the child widget from the 'MDHeroFrom' + # container. + hero_from_widget.remove_widget(hero_widget) + + # We set the size, position of the child widget of the + # 'MDHeroFrom' container and add this widget to the + # root window. + hero_widget.pos = self.screen_out.to_widget( + *hero_from_widget.to_window(*hero_from_widget.pos) + ) + hero_widget.size = hero_from_widget.size + self.manager.get_root_window().add_widget(hero_widget) + + # Animating widgets added to the root window. + if self.screen_in.heroes_to: + for hero_to_widget in self.screen_in.heroes_to: + self._check_hero_to_widget_tag( + hero_to_widget, hero_from_widget + ) + if hero_to_widget.tag == heroes_tag: + Animation( + size=hero_to_widget.size, + d=self.duration, + pos=hero_to_widget.pos, + ).start(hero_widget) + hero_from_widget.dispatch( + "on_transform_in", + hero_widget, + self.duration, + ) def animated_hero_out(self) -> None: - if self.manager._heroes_data and self.manager.current_hero: - self.screen_out.hero_to.remove_widget(self.hero_widget) - self.manager.get_root_window().add_widget(self.hero_widget) + """Animates the flight of heroes from screen **B** to screen **A**.""" - self.hero_from_widget.dispatch( - "on_transform_out", self.hero_widget, self.duration - ) - Animation( - pos=self.screen_in.to_widget( - *self.hero_from_widget.to_window(*self.hero_from_widget.pos) - ), - size=self.hero_from_widget.size, - d=self.duration, - ).start(self.hero_widget) + if ( + self.manager._heroes_data + and self.manager.current_heroes + and self.screen_out.heroes_to + ): + + for heroes_tag in self.manager.current_heroes: + for hero_to_widget in self.screen_out.heroes_to: + if hero_to_widget.tag == heroes_tag: + hero_from_children = self._hero_from_widget_children[ + heroes_tag + ] + hero_to_widget.remove_widget(hero_from_children) + self.manager.get_root_window().add_widget( + hero_from_children + ) + + for ( + hero_from_widget + ) in self.manager.get_hero_from_widget(): + hero_from_widget.dispatch( + "on_transform_out", + self._hero_from_widget_children[ + hero_from_widget.tag + ], + self.duration, + ) + Animation( + pos=self.screen_in.to_widget( + *hero_from_widget.to_window( + *hero_from_widget.pos + ) + ), + size=hero_from_widget.size, + d=self.duration, + ).start( + self._hero_from_widget_children[ + hero_from_widget.tag + ] + ) def on_complete(self) -> None: + """ + Override method. + See :attr:`kivy.uix.screenmanager.TransitionBase.on_complete'. + """ + super().on_complete() + if self.manager._heroes_data and self.manager.current_heroes: + for hero_from_widget in self.manager.get_hero_from_widget(): + for heroes_tag in self.manager.current_heroes: + if heroes_tag == hero_from_widget.tag: + hero_from_children = self._hero_from_widget_children[ + heroes_tag + ] + self.manager.get_root_window().remove_widget( + hero_from_children + ) + + # Adding a child widget from the 'MDHeraFrom' container + # to the 'MDHeroTo' container. + if self._direction == "in": + for hero_to_widget in self.screen_in.heroes_to: + if hero_to_widget.tag == heroes_tag: + hero_to_widget.add_widget( + hero_from_children + ) + # Restores the child widget for the 'MDHeraFrom' + # container. + elif self._direction == "out": + hero_from_widget.add_widget(hero_from_children) + if self._direction == "out": self._direction = "in" - if self.manager._heroes_data and self.manager.current_hero: - self.manager.get_root_window().remove_widget(self.hero_widget) - self.hero_from_widget.add_widget(self.hero_widget) else: self._direction = "out" - if self.manager._heroes_data and self.manager.current_hero: - self.manager.get_root_window().remove_widget(self.hero_widget) - self.screen_in.hero_to.add_widget(self.hero_widget) - def _check_widget_properties(self): - if not self.screen_in.hero_to: + # Checks the attributes for the 'self.screen_in' screen. + # Called from the animated_hero_in method. + def _check_widget_properties(self, hero_from_widget: MDHeroFrom): + if not self.screen_in.heroes_to: raise Exception( - f"The `hero_to` attribute is not specified for screen {self.screen_in}" + f"The `heroes_to` attribute is not specified for screen " + f"{self.screen_in}" ) - if len(self.hero_from_widget.children) > 1: + # The 'MDHeroFrom' widget allows you to place only one widget in + # itself. + if len(hero_from_widget.children) > 1: raise Exception( - f"{self.hero_from_widget.__class__} accept only one widget" + f"{hero_from_widget.__class__} accept only one widget" ) + # For new API support. + def _check_hero_to_widget_tag( + self, hero_to_widget: MDHeroTo, hero_from_widget: MDHeroFrom + ) -> None: + if not hero_to_widget.tag: + Logger.warning( + "KivyMD: " + f"Set the tag '{hero_from_widget.tag}' " + f"for the {hero_to_widget} widget to the same " + f"as for the {hero_from_widget} widget" + ) + hero_to_widget.tag = hero_from_widget.tag + class MDSwapTransition(SwapTransition, MDTransitionBase): pass diff --git a/sbapp/kivymd/uix/widget.py b/sbapp/kivymd/uix/widget.py index 23e4b80..044722b 100644 --- a/sbapp/kivymd/uix/widget.py +++ b/sbapp/kivymd/uix/widget.py @@ -8,7 +8,7 @@ with some widget properties. For example: Widget ------ -.. code-block:: +.. code-block:: kv Widget: size_hint: .5, None @@ -25,7 +25,7 @@ Widget MDWidget -------- -.. code-block:: +.. code-block:: kv MDWidget: size_hint: .5, None @@ -34,10 +34,15 @@ MDWidget md_bg_color: app.theme_cls.primary_color """ +__all__ = ("MDWidget",) + +from kivy.uix.widget import Widget + from kivymd.uix import MDAdaptiveWidget +from kivymd.uix.behaviors import DeclarativeBehavior -class MDWidget(MDAdaptiveWidget): +class MDWidget(DeclarativeBehavior, MDAdaptiveWidget, Widget): """ See :class:`~kivy.uix.Widget` class documentation for more information. diff --git a/sbapp/kivymd/utils/fitimage.py b/sbapp/kivymd/utils/fitimage.py deleted file mode 100644 index 71d5fea..0000000 --- a/sbapp/kivymd/utils/fitimage.py +++ /dev/null @@ -1,19 +0,0 @@ -""" -FitImage -======== - -.. note:: See :class:`~kivymd.uix.fitimage.FitImage` for more information -""" - -__all__ = ("FitImage",) - -from kivy import Logger - -from kivymd.uix.fitimage import FitImage - -Logger.warning( - "FitImage: Note!" - "\nIn the near future the `FitImage` widget will be moved to the " - "`kivymd.uix.fitimage` package.\nUse import of this widget like this:" - "`from kivymd.uix.fitimage import FitImage`." -)