From b28ed6075bbf65fdb78bd09e4773ab2a6f25a936 Mon Sep 17 00:00:00 2001 From: Luke Barnard Date: Thu, 17 May 2018 18:15:34 +0100 Subject: [PATCH] Implement slightly hacky CSS soln. to thumbnail sizing As the slightly nicer alternative to fixupHeight being applied once we actually have a timelineWidth. The niceness comes from not needing timelineWidth, which means we can implement at render time with CSS. (Despite still calculating aspect ratios when we render.) --- res/css/views/messages/_MImageBody.scss | 13 +++- src/components/views/messages/MImageBody.js | 66 ++++++--------------- 2 files changed, 29 insertions(+), 50 deletions(-) diff --git a/res/css/views/messages/_MImageBody.scss b/res/css/views/messages/_MImageBody.scss index 1c809f0743..9f0e77f765 100644 --- a/res/css/views/messages/_MImageBody.scss +++ b/res/css/views/messages/_MImageBody.scss @@ -20,5 +20,14 @@ limitations under the License. } .mx_MImageBody_thumbnail { - max-width: 100%; -} \ No newline at end of file + position: absolute; + width: 100%; + height: 100%; + left: 0; + top: 0; +} + +.mx_MImageBody_thumbnail_container { + overflow: hidden; + position: relative; +} diff --git a/src/components/views/messages/MImageBody.js b/src/components/views/messages/MImageBody.js index 6cc492acf8..87fe8d906c 100644 --- a/src/components/views/messages/MImageBody.js +++ b/src/components/views/messages/MImageBody.js @@ -21,15 +21,15 @@ import PropTypes from 'prop-types'; import { MatrixClient } from 'matrix-js-sdk'; import MFileBody from './MFileBody'; -import ImageUtils from '../../../ImageUtils'; import Modal from '../../../Modal'; import sdk from '../../../index'; -import dis from '../../../dispatcher'; import { decryptFile } from '../../../utils/DecryptFile'; import Promise from 'bluebird'; import { _t } from '../../../languageHandler'; import SettingsStore from "../../../settings/SettingsStore"; +const THUMBNAIL_MAX_HEIGHT = 600; + export default class extends React.Component { displayName: 'MImageBody' @@ -48,14 +48,12 @@ export default class extends React.Component { constructor(props) { super(props); - this.onAction = this.onAction.bind(this); this.onImageError = this.onImageError.bind(this); this.onImageLoad = this.onImageLoad.bind(this); this.onImageEnter = this.onImageEnter.bind(this); this.onImageLeave = this.onImageLeave.bind(this); this.onClientSync = this.onClientSync.bind(this); this.onClick = this.onClick.bind(this); - this.fixupHeight = this.fixupHeight.bind(this); this._isGif = this._isGif.bind(this); this.state = { @@ -140,7 +138,6 @@ export default class extends React.Component { } onImageLoad() { - this.fixupHeight(); this.props.onWidgetLoad(); } @@ -176,7 +173,6 @@ export default class extends React.Component { } componentDidMount() { - this.dispatcherRef = dis.register(this.onAction); const content = this.props.mxEvent.getContent(); if (content.file !== undefined && this.state.decryptedUrl === null) { let thumbnailPromise = Promise.resolve(null); @@ -217,7 +213,6 @@ export default class extends React.Component { componentWillUnmount() { this.unmounted = true; - dis.unregister(this.dispatcherRef); this.context.matrixClient.removeListener('sync', this.onClientSync); this._afterComponentWillUnmount(); @@ -234,50 +229,25 @@ export default class extends React.Component { _afterComponentWillUnmount() { } - onAction(payload) { - if (payload.action === "timeline_resize") { - this.fixupHeight(); - } - } - - fixupHeight() { - if (!this.refs.image) { - console.warn(`Refusing to fix up height on ${this.displayName} with no image element`); - return; - } - - const content = this.props.mxEvent.getContent(); - const timelineWidth = this.refs.body.offsetWidth; - const maxHeight = 600; // let images take up as much width as they can so long as the height doesn't exceed 600px. - // the alternative here would be 600*timelineWidth/800; to scale them down to fit inside a 4:3 bounding box - - // FIXME: this will break on clientside generated thumbnails (as per e2e rooms) - // which may well be much smaller than the 800x600 bounding box. - - // FIXME: It will also break really badly for images with broken or missing thumbnails - - // FIXME: Because we don't know what size of thumbnail the server's actually going to send - // us, we can't even really layout the page nicely for it. Instead we have to assume - // it'll target 800x600 and we'll downsize if needed to make things fit. - - // console.log("trying to fit image into timelineWidth of " + this.refs.body.offsetWidth + " or " + this.refs.body.clientWidth); - let thumbHeight = null; - if (content.info) { - thumbHeight = ImageUtils.thumbHeight(content.info.w, content.info.h, timelineWidth, maxHeight); - } - this.refs.image.style.height = thumbHeight + "px"; - // console.log("Image height now", thumbHeight); - } - _messageContent(contentUrl, thumbUrl, content) { + const maxHeight = Math.min(THUMBNAIL_MAX_HEIGHT, content.info.h); + const maxWidth = content.info.w * maxHeight / content.info.h; const thumbnail = ( - {content.body} +
+ { /* Calculate aspect ratio, using %padding will size _container correctly */ } +
+ + { /* Thumbnail CSS class resizes to exactly container size with inline CSS + to restrict width */ } + {content.body} +
);