Skip to content
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

[Issue/605][Android]Apply OS font size scale change in editor #795

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:configChanges="keyboard|keyboardHidden|orientation|screenSize"
android:configChanges="keyboard|keyboardHidden|orientation|screenSize|fontScale"
Copy link
Contributor Author

@hotchemi hotchemi Mar 31, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is not directly related to the issue but to align condition with wordpress-android added fontScale to prevent activity recreation by fontScale change.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👋 @hotchemi , does this change make the demo app differ in behavior between Android and iOS?

I'm thinking that making the demo app behave closer to WPAndroid might not be a desirable goal. Instead, the demo app should be as simple as possible and ideally not hide any "mistakes" or "hacks" WPAndroid implements. Also, ideally the Android and iOS implementations of the demo app should be as identical as possible.

android:windowSoftInputMode="adjustResize">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,7 @@ public void setFontSize(ReactAztecText view, float fontSize) {
view.setTextSize(
TypedValue.COMPLEX_UNIT_PX,
(int) Math.ceil(PixelUtil.toPixelFromSP(fontSize)));
view.refreshText();
}

@ReactProp(name = ViewProps.FONT_FAMILY)
Expand Down
23 changes: 21 additions & 2 deletions react-native-aztec/src/AztecView.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import PropTypes from 'prop-types';
import React from 'react';
import ReactNative, {requireNativeComponent, ViewPropTypes, UIManager, ColorPropType, TouchableWithoutFeedback, Platform} from 'react-native';
import ReactNative, {requireNativeComponent, ViewPropTypes, UIManager, ColorPropType, TouchableWithoutFeedback, Platform, Dimensions} from 'react-native';
import TextInputState from 'react-native/lib/TextInputState';
import { getScaledFontSize } from './utils';

const AztecManager = UIManager.getViewManagerConfig('RCTAztecView');

Expand Down Expand Up @@ -32,6 +33,23 @@ class AztecView extends React.Component {
...ViewPropTypes, // include the default view properties
}

constructor() {
super();
this.state = { fontScale: Dimensions.get('window').fontScale };
}

_onDimensionsChange = dimensions => {
this.setState({ fontScale: dimensions.window.fontScale });
}

componentWillMount() {
Dimensions.addEventListener('change', this._onDimensionsChange);
}

componentWillUnmount() {
Dimensions.removeEventListener('change', this._onDimensionsChange);
}

dispatch(command, params) {
params = params || [];
UIManager.dispatchViewManagerCommand(
Expand Down Expand Up @@ -149,11 +167,12 @@ class AztecView extends React.Component {
}

render() {
const { onActiveFormatsChange, onFocus, ...otherProps } = this.props
const { onActiveFormatsChange, onFocus, fontSize, ...otherProps } = this.props
return (
<TouchableWithoutFeedback onPress={ this._onPress }>
<RCTAztecView
{...otherProps}
fontSize={ getScaledFontSize(fontSize, this.state.fontScale) }
onContentSizeChange = { this._onContentSizeChange }
onHTMLContentWithCursor = { this._onHTMLContentWithCursor }
onSelectionChange = { this._onSelectionChange }
Expand Down
12 changes: 12 additions & 0 deletions react-native-aztec/src/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { Platform } from 'react-native';

// Basically React Native has a mechanism to scale font size automatically.
// But GM is rendered on top of fragment of which retain flag is true.
// Hence we deal with fontScale change manually on our side.
export function getScaledFontSize( fontSize, fontScale ) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

currently there's no dependency of unit test in react-native-aztec but if ok I'd like to install jest and write an unit test for the function🙇

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's hold on a bit while we're still assessing the solution in this PR.

While at it though, what do you have in mind for how such a unit test would be run? Will it become part of the Jest testsuite already in place in gutenberg-mobile?

if ( Platform.OS === 'ios' ) {
return fontSize ? fontSize : undefined; // to not to affect to iOS
}
const baseFontSize = 18;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a bit tricky but I did reverse-engineering to find out 18 is the base fontSize for paragraph block. If we use 14 you'll see the base font size is smaller than before.

no fontSize specify baseFontSize=18

But yea it's too tricky...

return ( fontSize ? fontSize : baseFontSize ) * fontScale;
}