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

Set the font size before opening the documents #7185

Merged
merged 7 commits into from
Mar 14, 2014
Merged

Conversation

TomMalbran
Copy link
Contributor

This is a fix for issue #7093. The idea is to add the styles with the new font-size and line-height before opening any document. To do this, when changing the font-size, we now save the strings that we use to set the styles, so we can use them to set the styles without having any editor open.

* @private
* Sets the font size and restores the scroll position as best as possible.
* @param {string} fontSizeStyle A string with the font size and the size unit
* @param {string} lineHeightStyle A string with the line height and a the size unit
Copy link
Contributor

Choose a reason for hiding this comment

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

Old typo: a the size unit. should remove a.

@RaymondLim
Copy link
Contributor

I verified that #7093 is fixed for any scroll positions only if Word Wrap is off. We may need to fix #7168 sooner since changes in this pull request make the scroll position off by the number of wrapped lines.

Done with review and will merge after you fix the JSDoc nits.

@TomMalbran
Copy link
Contributor Author

This does fix all the reload issues, with or without word wrap on. And I checked with the steps in issue #7168 and is fixed too.

The issue that this doesn't fix yet is increasing the font-size with Word Wrap on, but the issue there is in how we restore the scroll position. I need to check later if I can fix that using some of the new CM apis.

@RaymondLim
Copy link
Contributor

Yes, I meant #7168 with some font-size adjustment. I didn't remember that I filed #7168 without any font-size adjustment.

@TomMalbran
Copy link
Contributor Author

@RaymondLim I fixed the comments and removed the fontSizeAdjustment view state since we don't really need it anymore. I also added a fix for the word wrap issue, but it does't work when an inline editor is partially visible at the top of the screen, but might not be that bad of an issue. Let me know if the fix works for you.

var editor = EditorManager.getCurrentFullEditor(),
oldWidth = editor._codeMirror.defaultCharWidth(),
scrollPos = editor.getScrollPos(),
line = editor._codeMirror.lineAtHeight(scrollPos.y, "local");
Copy link
Contributor

Choose a reason for hiding this comment

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

cmv4 is already landed in master and I wonder lineAtHeight and heightAtLine are still the same in cmv4. Can you double check or can you merge master to your pull request?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure will try, hopefully it should behave the same way.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Actually I made this PR after cmv4 landed.

Copy link
Contributor

Choose a reason for hiding this comment

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

Really, but I notice that CM submodule is different.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, I can use multiple cursors in this branch.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ok.

@TomMalbran
Copy link
Contributor Author

@RaymondLim Changes pushed.

lhStr = PreferencesManager.getViewState("lineHeightStyle");

if (fsStr && lhStr) {
_removeDynamicFontSize();
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I left this remove here, even though we don't need it on the initial load, but it is required if this method is ever called after appReady.

Copy link
Contributor

Choose a reason for hiding this comment

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

Perfectly fine to call it here to ensure that we don't miss any.

@RaymondLim
Copy link
Contributor

@TomMalbran My changes are pushed. I'm done reviewing your changes and it's your turn to review mine. @dangoor Can you also review my changes in PreferenceStorage.js?

@TomMalbran
Copy link
Contributor Author

@RaymondLim The ViewCommands code looks good. I am not that familiar with the new Preferences code to know if your changes are good or not.

Edit: nvm the last comment, we only convert from the local storage.

@RaymondLim
Copy link
Contributor

@TomMalbran Yes, we're migrating only from the local storage. But your point is valid since this pull request is not going to release 37 and we may have a gap in migrating "fontSizeAdjustment" from release 37. Maybe we should stick with "fontSizeAdjustment" and just recalculate font size and line height in your new restoreFontSize function.

@MiguelCastillo
Copy link
Contributor

This may or may not be the proper place for this question. But would it be possible to expose the font sizing functionality as an API? I have added the ability to change the font size and type in Themes and it would be really smooth if I could leverage Brackets API instead of Themes completely taking over it. Any thoughts?

@TomMalbran
Copy link
Contributor Author

@RaymondLim That can be an option, but we would need to add the default font size as a new constant and remember to change it if we ever change it in the CSS. Anyway we already have that problem with the line height. Another option would be to do a 1 time conversion from the fontSizeAdjustment state view?

@MiguelCastillo To increase or decrease the font size, you could always call the commands. Or maybe even change the view states.

@MiguelCastillo
Copy link
Contributor

@TomMalbran Yeah I saw I could do that. I was looking for a direct way to set a specific font size.

@TomMalbran
Copy link
Contributor Author

@MiguelCastillo Wouldn't you just set the font size in the CSS, and have a function to increase it? This code should work for themes with other font-sizes.

@RaymondLim I just realized that it will be a bad idea to have the default font-size here, since we would have problems with themes that set the font-size to a different value in the CSS. We can get rid of the line height part by just setting the line height to 1.25 directly in the CSS, and other themes can set it to other unitless-values if required.

@MiguelCastillo
Copy link
Contributor

Ok, so here is how themes is working. I have an input field where you can set a particular font size you want to use. There is no button to increase/decrease the size. So, is there a way to tell bracket to set the font size to a specific number/unit? If not, that's no problem. I will just continue to override the style that brackets puts in the document.

@RaymondLim
Copy link
Contributor

@TomMalbran I like the new view states for saving us from recalculating font size and line height, but I don't like the fact that they are two independent view states. Instead of "fontSizeStyle" and "lineHeightStyle", we should group them just like we have with "linting" preferences. I know that linting preferences have some issues that @dangoor is looking at. @dangoor Can you suggest which solution we should follow?

@TomMalbran
Copy link
Contributor Author

@RaymondLim I don't like the way linting was done, since we end up using a . for both extension preferences and default ones. We have lots of preferences without adding a . (like all the editor ones) and some with it. We aren't consistent with it.

Anyway, what about just removing the whole line height part and place it into the CSS? if we just set it to 1.25, then we wouldn't need to update it everytime we update the font-size.

@MiguelCastillo No we don't have that function. If _adjustFontSize was exported, you could use it by passing the difference between the theme's default font-size and the new value. WE would need to change the function for it to accept a font-size and calculate the adjustment. Anyway, ins't it easier to just use the increase/decrease commands? Or maybe you could make a PR that would let you change the value (a menu item that makes a dialog appear, where you set the font-size).

@MiguelCastillo
Copy link
Contributor

@TomMalbran Yeap, the dialog to set the specific font size is what I have added in Themes as a configurable setting. So, if brackets provided that then there is no need for that setting in themes. Are the values that are stored by brackets in the preference font sizes or font increments?

@TomMalbran
Copy link
Contributor Author

@MiguelCastillo The current stored values are font increments, but this PR will change them to font sizes which include the units, so it would save something like 15px. Even with that, we never assume a default font-size, so we calculate the font-size using an increment. Anyway we can get the default font-size in the same was as it is done in _adjustFontSize (once an editor is open).

@TomMalbran
Copy link
Contributor Author

@MiguelCastillo Or maybe even easier would be to use _setSizeAndRestoreScroll(fsStr, lhStr); passing the parsed font-size and line-height.

@MiguelCastillo
Copy link
Contributor

@TomMalbran we are now thinking the same thing. :) Would you like me to open a PR?

@TomMalbran
Copy link
Contributor Author

@MiguelCastillo If you want to. But might be easier to do after we merge this one.

@MiguelCastillo
Copy link
Contributor

@TomMalbran I will do a PR. Thanks dude!

@RaymondLim
Copy link
Contributor

@TomMalbran I like the idea of removing the whole line height part and place it into the CSS. Can you make that change? I'll add the code to do the one-time conversion.

@TomMalbran
Copy link
Contributor Author

@RaymondLim Yes, I can do it soon.

@RaymondLim
Copy link
Contributor

@TomMalbran Actually, can you put the following change in your commit as it also depends on your changes?

    function restoreFontSize() {
        var fsStr = PreferencesManager.getViewState("fontSizeStyle"),
            fsAdjustment = PreferencesManager.getViewState("fontSizeAdjustment");

        if (fsAdjustment) {
            // Always remove the old view state even if we also have the new view state.
            PreferencesManager.setViewState("fontSizeAdjustment");

            if (!fsStr) {
                // Migrate the old view state to the new one.
                fsStr = (12 + fsAdjustment) + "px";
                PreferencesManager.setViewState("fontSizeStyle", fsStr);
            }
        }

        if (fsStr) {
            _removeDynamicFontSize();
            _addDynamicFontSize(fsStr);
        }
    }

@TomMalbran
Copy link
Contributor Author

@RaymondLim I removed the line height and added your code.

@@ -144,10 +144,8 @@
*/
.code-font() {
color: @content-color;
// line-height must be specified in px not em because the code font and line number font sizes are different.
// Sizing via em will cause the code and line numbers to misalign
line-height: 15px;
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 comment is no longer valid, since the line numbers are now in the same div as the rest of the line. Also if we wanted to decrease the font-size value of the line numbers, we can easily increase the line-height value.

@dangoor
Copy link
Contributor

dangoor commented Mar 14, 2014

@RaymondLim that preference conversion seems fine to me.

@@ -231,6 +195,30 @@ define(function (require, exports, module) {
}
}

/**
* Restores the Font Size and Line Height using the saved strings
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: We can take out Line Height here.

@RaymondLim
Copy link
Contributor

@TomMalbran Done with re-review. ready to merge after you fix the minor nit and the magic number.

@TomMalbran
Copy link
Contributor Author

@RaymondLim Fixes pushed

* @return {Object} JSON object for the new view state equivalent to
* the old "fontSizeAdjustment" preference.
*/
function _convertToNewViewStates(key, value) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Nice job in cleaning. Just one more minor nit since we only have one new view state, can you also rename this to singular _convertToNewViewState and update the callback argument in convertPreferences call below?

@TomMalbran
Copy link
Contributor Author

@RaymondLim done

@RaymondLim
Copy link
Contributor

Nice job! I really like the changes to new view state. Merging now.

RaymondLim added a commit that referenced this pull request Mar 14, 2014
Set the font size before opening the documents
@RaymondLim RaymondLim merged commit 8971aed into master Mar 14, 2014
@RaymondLim RaymondLim deleted the tom/issue-7093 branch March 14, 2014 23:07
@MiguelCastillo MiguelCastillo mentioned this pull request May 8, 2014
8 tasks
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants