-
Notifications
You must be signed in to change notification settings - Fork 703
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Launcher: support Component icons inside apworlds #3629
Changes from 2 commits
32ccaec
cfc568b
b3a2d75
a25f083
065cbc5
e71e076
17c1b6a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,8 @@ | |
import sys | ||
import typing | ||
import re | ||
import io | ||
import pkgutil | ||
from collections import deque | ||
|
||
if sys.platform == "win32": | ||
|
@@ -35,6 +37,7 @@ | |
from kivy.core.window import Window | ||
from kivy.core.clipboard import Clipboard | ||
from kivy.core.text.markup import MarkupLabel | ||
from kivy.core.image import ImageLoader, ImageLoaderBase, ImageData | ||
from kivy.base import ExceptionHandler, ExceptionManager | ||
from kivy.clock import Clock | ||
from kivy.factory import Factory | ||
|
@@ -61,6 +64,7 @@ | |
from kivy.uix.recycleview.layout import LayoutSelectionBehavior | ||
from kivy.animation import Animation | ||
from kivy.uix.popup import Popup | ||
from kivy.uix.image import AsyncImage | ||
|
||
fade_in_animation = Animation(opacity=0, duration=0) + Animation(opacity=1, duration=0.25) | ||
|
||
|
@@ -770,6 +774,42 @@ def fix_heights(self): | |
element.height = max_height | ||
|
||
|
||
class ApAsyncImage(AsyncImage): | ||
def is_uri(self, filename: str) -> bool: | ||
if filename.startswith("ap:"): | ||
return True | ||
else: | ||
return super().is_uri(filename) | ||
|
||
|
||
class ImageLoaderPkgutil(ImageLoaderBase): | ||
def load(self, filename: str) -> typing.List[ImageData]: | ||
# take off the "ap:" prefix | ||
module, path = filename[3:].split("/", 1) | ||
data = pkgutil.get_data(module, path) | ||
return self._bytes_to_data(data) | ||
|
||
def _bytes_to_data(self, data: typing.Union[bytes, bytearray]) -> typing.List[ImageData]: | ||
from PIL import Image as PImage | ||
p_im = PImage.open(io.BytesIO(data)).convert("RGBA") | ||
im_d = ImageData(p_im.size[0], p_im.size[1], p_im.mode.lower(), p_im.tobytes()) | ||
return [im_d] | ||
|
||
|
||
# grab the default loader method so we can override it but use it as a fallback | ||
DefaultLoad = ImageLoader.load | ||
|
||
|
||
def load_override(filename, default_load=DefaultLoad, **kwargs): | ||
qwint marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if filename[:3] == "ap:": | ||
qwint marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return ImageLoaderPkgutil(filename) | ||
else: | ||
return default_load(filename, **kwargs) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we're overriding There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if ImageLoader.load() needs to load asynchronously it returns an ImageLoaderBase that can handle the request |
||
|
||
|
||
ImageLoader.load = load_override | ||
|
||
|
||
class E(ExceptionHandler): | ||
logger = logging.getLogger("Client") | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
variable name
_
)maybe
_original_load
or_original_image_load
or_original_image_loader_load