Added LXMF image reception and display

This commit is contained in:
Mark Qvist 2024-03-10 18:14:04 +01:00
parent 1e86b266a9
commit bc2e524ec9

View File

@ -4,6 +4,7 @@ import LXMF
from kivy.metrics import dp,sp from kivy.metrics import dp,sp
from kivy.core.clipboard import Clipboard from kivy.core.clipboard import Clipboard
from kivy.core.image import Image as CoreImage
from kivymd.uix.card import MDCard from kivymd.uix.card import MDCard
from kivymd.uix.menu import MDDropdownMenu from kivymd.uix.menu import MDDropdownMenu
# from kivymd.uix.behaviors import RoundedRectangularElevationBehavior, FakeRectangularElevationBehavior # from kivymd.uix.behaviors import RoundedRectangularElevationBehavior, FakeRectangularElevationBehavior
@ -22,6 +23,7 @@ if RNS.vendor.platformutils.get_platform() == "android":
else: else:
from .helpers import multilingual_markup from .helpers import multilingual_markup
import io
import os import os
import plyer import plyer
import subprocess import subprocess
@ -199,6 +201,7 @@ class Messages():
extra_content = "" extra_content = ""
extra_telemetry = {} extra_telemetry = {}
telemeter = None telemeter = None
image_field = None
force_markup = False force_markup = False
signature_valid = False signature_valid = False
@ -233,6 +236,12 @@ class Messages():
except Exception as e: except Exception as e:
pass pass
if "lxm" in m and m["lxm"] and m["lxm"].fields != None and LXMF.FIELD_IMAGE in m["lxm"].fields:
try:
image_field = m["lxm"].fields[LXMF.FIELD_IMAGE]
except Exception as e:
pass
rcvd_d_str = "" rcvd_d_str = ""
trcvd = telemeter.read("received") if telemeter else None trcvd = telemeter.read("received") if telemeter else None
@ -328,14 +337,36 @@ class Messages():
md_bg_color=msg_color, md_bg_color=msg_color,
) )
def check_texture(w, val): if image_field != None:
item.has_image = True
img = item.ids.message_image
img.source = ""
img.texture = CoreImage(io.BytesIO(image_field[1]), ext=image_field[0]).texture
img.reload()
else:
item.has_image = False
def check_textures(w, val):
try: try:
if w.texture_size[1] > 360 and w.texture_size[1] >= self.max_texture_size: if w.texture_size[1] > 360 and w.texture_size[1] >= self.max_texture_size:
w.text = "[i]The content of this message is too large to display in the message stream. You can copy the message content into another program by using the context menu of this message, and selecting [b]Copy[/b].[/i]" w.text = "[i]The content of this message is too large to display in the message stream. You can copy the message content into another program by using the context menu of this message, and selecting [b]Copy[/b].[/i]"
except:
pass
item.ids.content_text.bind(texture_size=check_texture) if w.owner.has_image:
img = w.owner.ids.message_image
img.size_hint_x = 1
img.size_hint_y = None
img_w = w.owner.size[0]
img_ratio = img.texture_size[0] / img.texture_size[1]
img.size = (img_w,img_w/img_ratio)
img.fit_mode = "contain"
except Exception as e:
RNS.log("An error occurred while scaling message display textures:", RNS.LOG_ERROR)
RNS.trace_exception(e)
item.ids.content_text.owner = item
item.ids.content_text.bind(texture_size=check_textures)
if not RNS.vendor.platformutils.is_android(): if not RNS.vendor.platformutils.is_android():
item.radius = dp(5) item.radius = dp(5)
@ -759,7 +790,7 @@ Builder.load_string("""
padding: dp(8) padding: dp(8)
radius: dp(4) radius: dp(4)
size_hint: 1.0, None size_hint: 1.0, None
height: content_text.height + heading_text.height + dp(32) height: content_text.height + heading_text.height + message_image.size[1] + dp(32)
pos_hint: {"center_x": .5, "center_y": .5} pos_hint: {"center_x": .5, "center_y": .5}
MDRelativeLayout: MDRelativeLayout:
@ -783,6 +814,12 @@ Builder.load_string("""
# text_color: rgba(255,255,255,100) # text_color: rgba(255,255,255,100)
pos: 0, root.height - (self.height + root.padding[0] + dp(8)) pos: 0, root.height - (self.height + root.padding[0] + dp(8))
Image:
id: message_image
size_hint_x: 0
size_hint_y: 0
pos: 0, root.height - (self.height + root.padding[0] + dp(8)) - heading_text.height - dp(8)
MDLabel: MDLabel:
id: content_text id: content_text
text: root.text text: root.text