Skip to content
This repository has been archived by the owner on Sep 11, 2024. It is now read-only.

Commit

Permalink
Add call tiles
Browse files Browse the repository at this point in the history
  • Loading branch information
robintown committed Sep 30, 2022
1 parent 7a33818 commit ed3c939
Show file tree
Hide file tree
Showing 17 changed files with 552 additions and 51 deletions.
3 changes: 2 additions & 1 deletion res/css/_components.pcss
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@
@import "./views/elements/_Validation.pcss";
@import "./views/emojipicker/_EmojiPicker.pcss";
@import "./views/location/_LocationPicker.pcss";
@import "./views/messages/_CallEvent.pcss";
@import "./views/messages/_CreateEvent.pcss";
@import "./views/messages/_DateSeparator.pcss";
@import "./views/messages/_DisambiguatedProfile.pcss";
Expand Down Expand Up @@ -264,6 +265,7 @@
@import "./views/rooms/_JumpToBottomButton.pcss";
@import "./views/rooms/_LinkPreviewGroup.pcss";
@import "./views/rooms/_LinkPreviewWidget.pcss";
@import "./views/rooms/_LiveContentSummary.pcss";
@import "./views/rooms/_MemberInfo.pcss";
@import "./views/rooms/_MemberList.pcss";
@import "./views/rooms/_MessageComposer.pcss";
Expand All @@ -285,7 +287,6 @@
@import "./views/rooms/_RoomPreviewCard.pcss";
@import "./views/rooms/_RoomSublist.pcss";
@import "./views/rooms/_RoomTile.pcss";
@import "./views/rooms/_RoomTileCallSummary.pcss";
@import "./views/rooms/_RoomUpgradeWarningBar.pcss";
@import "./views/rooms/_SearchBar.pcss";
@import "./views/rooms/_SendMessageComposer.pcss";
Expand Down
1 change: 1 addition & 0 deletions res/css/views/elements/_FacePile.pcss
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ limitations under the License.
display: inline-flex;
flex-direction: row-reverse;
vertical-align: middle;
margin: 0 -1px; /* to cancel out the border on the edges */

/* Overlap the children */
> * + * {
Expand Down
79 changes: 79 additions & 0 deletions res/css/views/messages/_CallEvent.pcss
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
Copyright 2022 The Matrix.org Foundation C.I.C.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

.mx_CallEvent_wrapper {
display: flex;
width: 100%;
}

.mx_CallEvent {
padding: 12px;
box-sizing: border-box;
min-height: 60px;
max-width: 600px;
width: 100%;
background-color: $system;
border-radius: 8px;

display: flex;
align-items: center;
justify-content: space-between;
gap: $spacing-8;

.mx_CallEvent_title {
font-size: $font-15px;
line-height: 24px; /* in px to match the avatar */
}

&.mx_CallEvent_inactive .mx_CallEvent_title::before {
display: inline-block;
vertical-align: middle;
content: '';
background-color: $secondary-content;
mask-image: url('$(res)/img/element-icons/call/video-call.svg');
mask-size: 16px;
width: 16px;
height: 16px;
margin-right: 8px;
}

&.mx_CallEvent_active .mx_CallEvent_title {
font-weight: 600;
}

> .mx_BaseAvatar {
align-self: flex-start;
}

> .mx_CallEvent_infoRows {
flex-grow: 1;

display: flex;
flex-direction: column;
gap: $spacing-4;
}

> .mx_CallEvent_duration {
padding: $spacing-4;
color: $secondary-content;
font-size: $font-12px;
}

> .mx_CallEvent_button {
box-sizing: border-box;
min-width: 120px;
}
}
3 changes: 2 additions & 1 deletion res/css/views/rooms/_EventBubbleTile.pcss
Original file line number Diff line number Diff line change
Expand Up @@ -523,7 +523,8 @@ limitations under the License.
max-width: 100%;
}

.mx_LegacyCallEvent_wrapper {
.mx_LegacyCallEvent_wrapper,
.mx_CallEvent_wrapper {
justify-content: center;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,26 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

.mx_RoomTileCallSummary {
.mx_RoomTileCallSummary_text {
.mx_LiveContentSummary {
color: $secondary-content;

.mx_LiveContentSummary_text {
&::before {
display: inline-block;
vertical-align: text-bottom;
content: '';
background-color: $secondary-content;
mask-image: url('$(res)/img/element-icons/call/video-call.svg');
mask-size: 16px;
width: 16px;
height: 16px;
margin-right: 4px;
}

&.mx_RoomTileCallSummary_text_active {
&.mx_LiveContentSummary_text_video::before {
mask-image: url('$(res)/img/element-icons/call/video-call.svg');
}

&.mx_LiveContentSummary_text_active {
color: $accent;

&::before {
Expand All @@ -37,7 +42,7 @@ limitations under the License.
}
}

.mx_RoomTileCallSummary_participants::before {
.mx_LiveContentSummary_participants::before {
display: inline-block;
vertical-align: text-bottom;
content: '';
Expand Down
10 changes: 5 additions & 5 deletions src/components/views/elements/AccessibleButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ type IProps<T extends keyof JSX.IntrinsicElements> = DynamicHtmlElementProps<T>
disabled?: boolean;
className?: string;
triggerOnMouseDown?: boolean;
onClick(e?: ButtonEvent): void | Promise<void>;
onClick: ((e: ButtonEvent) => void | Promise<void>) | null;
};

interface IAccessibleButtonProps extends React.InputHTMLAttributes<Element> {
Expand Down Expand Up @@ -105,9 +105,9 @@ export default function AccessibleButton<T extends keyof JSX.IntrinsicElements>(
newProps["disabled"] = true;
} else {
if (triggerOnMouseDown) {
newProps.onMouseDown = onClick;
newProps.onMouseDown = onClick ?? undefined;
} else {
newProps.onClick = onClick;
newProps.onClick = onClick ?? undefined;
}
// We need to consume enter onKeyDown and space onKeyUp
// otherwise we are risking also activating other keyboard focusable elements
Expand All @@ -123,7 +123,7 @@ export default function AccessibleButton<T extends keyof JSX.IntrinsicElements>(
case KeyBindingAction.Enter:
e.stopPropagation();
e.preventDefault();
return onClick(e);
return onClick?.(e);
case KeyBindingAction.Space:
e.stopPropagation();
e.preventDefault();
Expand All @@ -143,7 +143,7 @@ export default function AccessibleButton<T extends keyof JSX.IntrinsicElements>(
case KeyBindingAction.Space:
e.stopPropagation();
e.preventDefault();
return onClick(e);
return onClick?.(e);
default:
onKeyUp?.(e);
break;
Expand Down
192 changes: 192 additions & 0 deletions src/components/views/messages/CallEvent.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,192 @@
/*
Copyright 2022 The Matrix.org Foundation C.I.C.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

import React, { FC, forwardRef, useCallback, useContext, useEffect, useMemo, useState } from "react";

import type { MatrixEvent } from "matrix-js-sdk/src/models/event";
import type { RoomMember } from "matrix-js-sdk/src/models/room-member";
import { Call, ConnectionState } from "../../../models/Call";
import { _t } from "../../../languageHandler";
import { useCall, useConnectionState, useParticipants } from "../../../hooks/useCall";
import defaultDispatcher from "../../../dispatcher/dispatcher";
import type { ViewRoomPayload } from "../../../dispatcher/payloads/ViewRoomPayload";
import { Action } from "../../../dispatcher/actions";
import type { ButtonEvent } from "../elements/AccessibleButton";
import MemberAvatar from "../avatars/MemberAvatar";
import { LiveContentSummary, LiveContentType } from "../rooms/LiveContentSummary";
import FacePile from "../elements/FacePile";
import { formatCallTime } from "../../../DateUtils";
import AccessibleButton from "../elements/AccessibleButton";
import MatrixClientContext from "../../../contexts/MatrixClientContext";

interface CallEventDurationProps {
delta: number;
}

const CallEventDuration: FC<CallEventDurationProps> = ({ delta }) => {
// Clock desync could lead to a negative duration, so just hide it if that happens
if (delta <= 0) return null;
return <div className="mx_CallEvent_duration">{ formatCallTime(new Date(delta)) }</div>;
};

const MAX_FACES = 8;

interface ActiveCallEventProps {
mxEvent: MatrixEvent;
participants: Set<RoomMember>;
buttonText: string;
buttonKind: string;
onButtonClick: ((ev: ButtonEvent) => void) | null;
}

const ActiveCallEvent = forwardRef<any, ActiveCallEventProps>(
(
{
mxEvent,
participants,
buttonText,
buttonKind,
onButtonClick,
},
ref,
) => {
const senderName = useMemo(() => mxEvent.sender?.name ?? mxEvent.getSender(), [mxEvent]);

const facePileMembers = useMemo(() => [...participants].slice(0, MAX_FACES), [participants]);
const facePileOverflow = participants.size > facePileMembers.length;

const [now, setNow] = useState(() => Date.now());
useEffect(() => {
const timer = setInterval(() => setNow(Date.now()), 1000);
return () => clearInterval(timer);
}, []);

return <div className="mx_CallEvent_wrapper" ref={ref}>
<div className="mx_CallEvent mx_CallEvent_active">
<MemberAvatar
member={mxEvent.sender}
fallbackUserId={mxEvent.getSender()}
viewUserOnClick
width={24}
height={24}
/>
<div className="mx_CallEvent_infoRows">
<span className="mx_CallEvent_title">
{ _t("%(name)s started a video call", { name: senderName }) }
</span>
<LiveContentSummary
type={LiveContentType.Video}
text={_t("Video call")}
active={false}
participantCount={participants.size}
/>
<FacePile members={facePileMembers} faceSize={24} overflow={facePileOverflow} />
</div>
<CallEventDuration delta={now - mxEvent.getTs()} />
<AccessibleButton
className="mx_CallEvent_button"
kind={buttonKind}
disabled={onButtonClick === null}
onClick={onButtonClick}
>
{ buttonText }
</AccessibleButton>
</div>
</div>;
},
);

interface ActiveLoadedCallEventProps {
mxEvent: MatrixEvent;
call: Call;
}

const ActiveLoadedCallEvent = forwardRef<any, ActiveLoadedCallEventProps>(({ mxEvent, call }, ref) => {
const connectionState = useConnectionState(call);
const participants = useParticipants(call);

const connect = useCallback((ev: ButtonEvent) => {
ev.preventDefault();
defaultDispatcher.dispatch<ViewRoomPayload>({
action: Action.ViewRoom,
room_id: mxEvent.getRoomId()!,
view_call: true,
metricsTrigger: undefined,
});
}, [mxEvent]);

const disconnect = useCallback((ev: ButtonEvent) => {
ev.preventDefault();
call.disconnect();
}, [call]);

const [buttonText, buttonKind, onButtonClick] = useMemo(() => {
switch (connectionState) {
case ConnectionState.Disconnected: return [_t("Join"), "primary", connect];
case ConnectionState.Connecting: return [_t("Join"), "primary", null];
case ConnectionState.Connected: return [_t("Leave"), "danger", disconnect];
case ConnectionState.Disconnecting: return [_t("Leave"), "danger", null];
}
}, [connectionState, connect, disconnect]);

return <ActiveCallEvent
ref={ref}
mxEvent={mxEvent}
participants={participants}
buttonText={buttonText}
buttonKind={buttonKind}
onButtonClick={onButtonClick}
/>;
});

interface CallEventProps {
mxEvent: MatrixEvent;
}

/**
* An event tile representing an active or historical Element call.
*/
export const CallEvent = forwardRef<any, CallEventProps>(({ mxEvent }, ref) => {
const noParticipants = useMemo(() => new Set<RoomMember>(), []);
const client = useContext(MatrixClientContext);
const call = useCall(mxEvent.getRoomId()!);
const latestEvent = client.getRoom(mxEvent.getRoomId())!.currentState
.getStateEvents(mxEvent.getType(), mxEvent.getStateKey()!);

if ("m.terminated" in latestEvent.getContent()) {
// The call is terminated
return <div className="mx_CallEvent_wrapper" ref={ref}>
<div className="mx_CallEvent mx_CallEvent_inactive">
<span className="mx_CallEvent_title">{ _t("Video call ended") }</span>
<CallEventDuration delta={latestEvent.getTs() - mxEvent.getTs()} />
</div>
</div>;
}

if (call === null) {
// There should be a call, but it hasn't loaded yet
return <ActiveCallEvent
ref={ref}
mxEvent={mxEvent}
participants={noParticipants}
buttonText={_t("Join")}
buttonKind="primary"
onButtonClick={null}
/>;
}

return <ActiveLoadedCallEvent mxEvent={mxEvent} call={call} ref={ref} />;
});
Loading

0 comments on commit ed3c939

Please sign in to comment.